The isAssignableFrom() method of java.lang.Class class is used to check if the specified class’s object is compatible to be assigned to the instance of this Class. It will be compatible if both the classes are the same, or the specified class is a superclass or superinterface. The method returns true if the specified class’s object can be cast to the instance of this Class. It returns false otherwise.
public boolean isAssignableFrom(Class<T> class)
Parameter: This method accepts class as parameter which is the specified class whose object to checked for compatibility to this Class instance.
Return Value: This method returns true if the specified class’s object can be cast to the instance of this Class. It returns false otherwise.
Below programs demonstrate the isAssignableFrom() method.
Class represented by myClass: class Test Class represented by c: class java.lang.String Is c compatible: false
Class represented by myClass: class Test Class represented by c: class Test Is c compatible: true
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.