getClass
returns the run time class of the particular object. Where as
instance of is used to check the super class of the object.
class A{
}
class B extends A{
}
class GetClassVsInstance{
public static void main(String args[]){
A obj1 = new A();
B obj2 = new B();
System.out.println("is obj2 is instance of Class B " + (obj2 instanceof B));
System.out.println("is obj2 is instance of Class A " + (obj2 instanceof A));
System.out.println("Run time class for obj2 is " +obj2.getClass());
}
}
Output
is obj2 is instance of Class B true is obj2 is instance of Class A true Run time class for obj2 is class B
No comments:
Post a Comment