Java Class asSubclass() Method

The asSubclass() method of java Class casts this Class object to represent a subclass of the class represented by the specified class object. It throws a ClassCastException if the cast is not valid otherwise it returns a reference to this(current) class object.

Syntax

Parameter

U - Type to cast this class object to

clazz - class of the type to cast this class object to.

Returns

This Class object and cast to represent a subclass of the specified class object).

Throws

ClassCastException

Example 1

Test it Now

Output:

class ClassasSubclassExample1
class SubClass
class ClassasSubclassExample1

Example 2

Test it Now

Output:

Derived Class Constructor........
class Class.ClassasSubclassExample2
class Class.SubClass1
class Class.SubClass1

Next TopicJava Class




Latest Courses