Home » Java programming language

Java Class class isAssignableFrom() method with example

Class class isAssignableFrom() method: Here, we are going to learn about the isAssignableFrom() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 02, 2019

Class class isAssignableFrom() method

  • isAssignableFrom() method is available in java.lang package.
  • isAssignableFrom() method is used to check whether the class or an interface denoted by this Class object is either the same as, or the Class object is a superclass or superinterface.
  • isAssignableFrom() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • isAssignableFrom() method may throw an exception at the time of assigning an object.
    NullPointerException: In the exception, when the given class exists null.


    public boolean isAssignableFrom(Class class);


  • Class class – represents the Class object to be determined.

Return value:

The return type of this method is boolean, it returns a boolean value based on the following cases,

  • It returns true, when the object of class is assignable to object of this Class.
  • It returns false, when the object of class is not assignable to object of this Class.


// Java program to demonstrate the example 
// of boolean isAssignableFrom(Class class) method of Class 

public class Parent {
    public static void main(String[] args) throws Exception {
        // Create and Return Parent Class object
        Parent p = new Parent();
        Class cl1 = p.getClass();

        // Create and Return Child Class object
        Child ch = new Child();
        Class cl2 = ch.getClass();

        // We are checking the given Parent class is 
        // Assignable from Child Class
        boolean child = cl2.isAssignableFrom(cl1);
        System.out.println("Is" + " " + cl1.getSimpleName() + " " + "Assignable from Child: " + " " + child);

        // We are checking the given Child class is 
        // Assignable from Parent Class
        boolean parent = cl1.isAssignableFrom(cl2);
        System.out.println("Is" + " " + cl2.getSimpleName() + " " + "Assignable from Parent: " + " " + parent);

class Child extends Parent {
    public Child() {
        // Default Constructor with blank implementation


Is Parent Assignable from Child:  false
Is Child Assignable from Parent:  true

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.