In Java, it is often required to analyze the allocated privileges or the updated permissions to the classes from time to time after inheritance. In such a case scenario, the “isAssignableFrom()” method in Java enables the programmer to manage the class permissions and keep the confidential records secure appropriately.
This tutorial discusses the working of the “java.lang.Class.isAssignableFrom()” method.
How to Use the “java.lang.Class.isAssignableFrom()” Method in Java?
The “isAssignableFrom()” method of the “java.lang.Class” class is applied to verify if the specific class’s object is compatible/supported to be assigned/allocated to the instance of the associated class. It is such that it retrieves “true” if both classes are identical/same, or the specific class is superinterface or superclass.
Syntax
boolean isAssignableFrom(Class<T> x)
In this given syntax, “x” corresponds to the specific class whose object is to be analyzed for compatibility with the associated class instance.
Return Value: It gives a Boolean value. For instance, it gives “true” if the specific class’s object can be allocated to the instance of the associated class. Else, it retrieves “false”.
Example: Applying the “java.lang.Class.isAssignableFrom()” Method in Java
This example applies the discussed method to the parent and the child classes to check if both can be assigned from each other individually:
public class AssignableFrom {
class childClass extends AssignableFrom {
}
public static void main(String[] args) {
try {
AssignableFrom obj = new AssignableFrom();
Class x = obj.getClass();
Class child = childClass.class;
boolean result = child.isAssignableFrom(x);
System.out.println("Is childClass Class Assignable from AssignableFrom Class? " + result);
result = x.isAssignableFrom(child);
System.out.println("Is AssignableFrom Class Assignable from childClass Class? " + result);
}
catch(Exception exc) {
System.out.println(exc.toString());
}
}}
According to these code lines, perform the below-given steps:
- Create a child class “childClass” that inherits from the parent class “AssignableFrom” via the “extends” keyword.
- In “main”, in the “try” block, create an object of the parent class and apply the “getClass()” method to fetch the runtime class of the object.
- After that, associate an object with the child class named “child”.
- Now, apply the “isAssignableFrom()” method to verify if the child class i.e., “childClass” is assignable from the parent class “AssignableFrom” via their objects and print the corresponding outcome.
- Likewise, implement the discussed method again to check if the parent class is assignable from the child class and display the output.
- Lastly, cope up with the exceptions(if any) in the “catch” block.
Output

From this output, it can be implied that the parent class i.e., “AssignableFrom” is assignable from the child class i.e., “childClass” while it is not the case the other way around.
Conclusion
The “isAssignableFrom()” method of the “java.lang.Class” class is applied to verify if the specific class’s object is compatible/supported to be allocated to the associated class’s instance. This guide demonstrated the working of the Java “isAssignableFrom()” method.