Skip to content

Instantly share code, notes, and snippets.

@tedheich
Created November 19, 2009 01:13
Show Gist options
  • Save tedheich/238441 to your computer and use it in GitHub Desktop.
Save tedheich/238441 to your computer and use it in GitHub Desktop.
How to find out the class of the object
class Shape {}
class Circle extends Shape {}
class Square extends Shape {}
class KnowType {
public static void main(String []args) {
Shape s = new Shape();
Circle c = new Circle();
Square sq = new Square();
System.out.println(s.getClass());
System.out.println(c.getClass());
System.out.println(sq.getClass());
}
}
class Shape {}
class Circle extends Shape {}
class Square extends Shape {}
class ShapeTest {
public static void main(String []args) {
Circle obj1 = new Circle();
Square obj2 = new Square();
if (obj1 instanceof Circle) {
System.out.println("It's a circle");
}
else if(obj1 instanceof Shape) {
System.out.println("It's a square");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment