Created
November 19, 2009 01:13
-
-
Save tedheich/238441 to your computer and use it in GitHub Desktop.
How to find out the class of the object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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