![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 asSubclass() Method
Description
The Java Class asSubclass() method casts this Class object to represent a subclass of the class represented by the specified class object. It is useful when a client needs to "narrow" the type of a Class object to pass it to an API that restricts the Class objects that it is willing to accept.
Declaration
Following is the declaration for java.lang.Class.asSubclass() method
public <U> Class<? extends U> asSubclass(Class<U> clazz)
Parameters
NA
Return Value
This method returns this Class object, cast to represent a subclass of the specified class object.
Exception
ClassCastException − if this Class object does not represent a subclass of the specified class (in this "subclass" includes the class itself).
Casting a Parent Class as SubClass Example
The following example shows the usage of Class asSubclass() method. We've created two classes, ClassDemo and its subclass SubClass1. Using getClass() method, we printed the class names and then using asSubClass we're casting parent class as child class and are getting its instance.
package com.tutorialspoint; public class ClassDemo { public static void main(String[] args) { try { ClassDemo cls = new ClassDemo(); ClassDemo subcls = new SubClass1(); // class ClassDemo Class c = cls.getClass(); System.out.println(c); // sub class SubClass1 Class c1 = subcls.getClass(); System.out.println(c1); // represent a subclass of the specified class object Class retval = c1.asSubclass(c); System.out.println(retval); } catch(ClassCastException e) { System.out.println(e.toString()); } } } class SubClass1 extends ClassDemo { // sub class }
Output
Let us compile and run the above program, this will produce the following result −
class com.tutorialspoint.ClassDemo class com.tutorialspoint.SubClass1 class com.tutorialspoint.SubClass1
Facing Exception while Casting a Parent Class as SubClass Example
The following example shows the usage of Class asSubclass() method. We've created two classes, ClassDemo and SubClass1. Using getClass() method, we printed the class names and then using asSubClass we're trying to casting ClassDemo class as child class and are getting exception.
package com.tutorialspoint; public class ClassDemo { public static void main(String[] args) { try { ClassDemo cls = new ClassDemo(); SubClass1 subcls = new SubClass1(); // class ClassDemo Class c = cls.getClass(); System.out.println(c); // sub class SubClass1 Class c1 = subcls.getClass(); System.out.println(c1); // represent a subclass of the specified class object Class retval = c1.asSubclass(c); System.out.println(retval); } catch(ClassCastException e) { System.out.println(e.toString()); } } } class SubClass1 { // sub class }
Output
Let us compile and run the above program, this will produce the following result −
class com.tutorialspoint.ClassDemo class com.tutorialspoint.SubClass1 java.lang.ClassCastException: class com.tutorialspoint.SubClass1