![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Class isAssignableFrom() Method
Description
The Java Class isAssignableFrom() method determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.
Declaration
Following is the declaration for java.lang.Class.isAssignableFrom() method
public boolean isAssignableFrom(Class<?> cls)
Parameters
cls − This is the Class object to be checked.
Return Value
This method returns the boolean value indicating whether objects of the type cls can be assigned to objects of this class.
Exception
NullPointerException − if the specified Class parameter is null.
Getting Assignable Status of a Non-Assignable Class Example
The following example shows the usage of java.lang.Class.isAssignableFrom() method. In this program, we've created an instance of ClassDemo and then using getClass() method, the class of the instance is retrieved. Then class of a sub class of ClassDemo is retrieved and using isAssignableFrom(), assignable status of SubClass is checked and printed.
package com.tutorialspoint; public class ClassDemo { public static void main(String[] args) { try { ClassDemo cls = new ClassDemo(); Class c = cls.getClass(); // class object associated with BaseClass Class subClass = SubClass.class; // checks whether BaseClass is assignable from ClassDemo boolean retval = subClass.isAssignableFrom(c); System.out.println("Return Value = " + retval); } catch(Exception e) { System.out.println(e.toString()); } } } // base class class SubClass extends ClassDemo { public SubClass() { // no argument constructor } }
Output
Let us compile and run the above program, this will produce the following result −
Return Value = false
Getting Assignable Status of a Assignable Class Example
The following example shows the usage of java.lang.Class.isAssignableFrom() method. In this program, we've created an instance of ClassDemo and then using getClass() method, the class of the instance is retrieved. Then class of a sub class of ClassDemo is retrieved and using isAssignableFrom(), assignable status of ClassDemo is checked and printed.
package com.tutorialspoint; public class ClassDemo { public static void main(String[] args) { try { ClassDemo cls = new ClassDemo(); Class c = cls.getClass(); // class object associated with BaseClass Class subClass = SubClass.class; // checks whether ClassDemo is assignable from BaseClass boolean retval = c.isAssignableFrom(subClass); System.out.println("Return Value = " + retval); } catch(Exception e) { System.out.println(e.toString()); } } } // base class class SubClass extends ClassDemo { public SubClass() { // no argument constructor } }
Output
Let us compile and run the above program, this will produce the following result −
Return Value = true