Summary :
- If you want to know whether or not a Class extends another, use Class#isAssignableFrom(Class)
- Class#isAssignableFrom(Class) also returns true if both classes are same
- To find if an object is instance of a class use instanceof operator
- To know if a class is direct sub class of another class then use Class#getSuperClass().equals(Class)
Setup :
We have an interface MyInterface and three classes MyBaseClass, MySubClass and SomeOtherClass with the below hierarchy.
interface MyInterface {
}
class MyBaseClass implements MyInterface {
}
class MySubClass extends MyBaseClass {
}
class SomeOtherClass {
}
Tests:
public class Test {
public static void main( String[] args ) {
/*
* Checking if a class is same as or is a superclass or superinterface
*
* of another class (the class in parameter)
*/
System.out.println( MyBaseClass.class.isAssignableFrom( MyBaseClass.class ) );// true : same class
System.out.println( MyBaseClass.class.equals( MyBaseClass.class ) );// true : same class
System.out.println( MyBaseClass.class.isAssignableFrom( MySubClass.class ) );// true : superclass
System.out.println( MyInterface.class.isAssignableFrom( MySubClass.class ) );// true : superinterface
System.out.println( MyBaseClass.class.isAssignableFrom( SomeOtherClass.class ) );// false : the two classes has no relation
System.out.println( MySubClass.class.isAssignableFrom( MyBaseClass.class ) );// false : MySubClass is not the superclass
/*
* Checking if a object is instance of a class
*/
IMyInterface object = new MyBaseClass( );
System.out.println( object instanceof MyInterface ); // true
System.out.println( object instanceof MyBaseClass ); // true
System.out.println( object instanceof MySubClass ); // false
System.out.println( object instanceof SomeOtherClass );// false
/*
* check if a class is direct superclass of another
*/
System.out.println( MySubClass.class.getSuperclass( ).equals( MyInterface.class ) );// false : its not a directsuper class, interface
System.out.println( MyBaseClass.class.getSuperclass( ).equals( MyInterface.class ) );// false : its not a directsuper class, interface
System.out.println( MySubClass.class.getSuperclass( ).equals( MyBaseClass.class ) );// true : MyBaseClass is extending MySubClass
}
}
Full code :
public class Test {
public static void main( String[] args ) {
/*
* Checking if a class is same as or is a superclass or superinterface
*
* of another class (the class in parameter)
*/
System.out.println( MyBaseClass.class.isAssignableFrom( MyBaseClass.class ) );// true : same class
System.out.println( MyBaseClass.class.equals( MyBaseClass.class ) );// true : same class
System.out.println( MyBaseClass.class.isAssignableFrom( MySubClass.class ) );// true : superclass
System.out.println( MyInterface.class.isAssignableFrom( MySubClass.class ) );// true : superinterface
System.out.println( MyBaseClass.class.isAssignableFrom( SomeOtherClass.class ) );// false : the two classes has no relation
System.out.println( MySubClass.class.isAssignableFrom( MyBaseClass.class ) );// false : MySubClass is not the superclass
/*
* Checking if a object is instance of a class
*/
IMyInterface object = new MyBaseClass( );
System.out.println( object instanceof MyInterface ); // true
System.out.println( object instanceof MyBaseClass ); // true
System.out.println( object instanceof MySubClass ); // false
System.out.println( object instanceof SomeOtherClass );// false
/*
* check if a class is direct superclass of another
*/
System.out.println( MySubClass.class.getSuperclass( ).equals( MyInterface.class ) );// false : its not a directsuper class, interface
System.out.println( MyBaseClass.class.getSuperclass( ).equals( MyInterface.class ) );// false : its not a directsuper class, interface
System.out.println( MySubClass.class.getSuperclass( ).equals( MyBaseClass.class ) );// true : MyBaseClass is extending MySubClass
}
}
interface MyInterface {
}
class MyBaseClass implements MyInterface {
}
class MySubClass extends MyBaseClass {
}
class SomeOtherClass {
}