java.lang.Class.isAssignableFrom()


java.lang.Class.isAssignableFrom()

package com.codingdict;



import java.lang.*;



public class ClassDemo {



   public static void main(String[] args) {



      try {

         ClassDemo cls = new ClassDemo();

         Class c = cls.getClass();



         // class object associated with BaseClass

         Class bClass = BaseClass.class;



         // checks whether BaseClass is assignable from ClassDemo

         boolean retval = bClass.isAssignableFrom(c);

         System.out.println("Return Value = " + retval);



         // checks whether ClassDemo is assignable from BaseClass

         retval = c.isAssignableFrom(bClass);

         System.out.println("Return Value  = " + retval);

      } catch(Exception e) {

         System.out.println(e.toString());

      }

   }

}



// base class

class BaseClass extends ClassDemo {



    public BaseClass() {

       // no argument constructor

    }

}