Java Class.asSubclass(Class <U> clazz)
Syntax
Class.asSubclass(Class <U> clazz) has the following syntax.
public <U> Class <? extends U> asSubclass(Class <U> clazz)
Example
In the following code shows how to use Class.asSubclass(Class <U> clazz) method.
//w ww . ja v a 2s. co m
public class Main {
public static void main(String[] args) throws Exception {
Main cls = new Main();
Main subcls = new SubClass1();
// class Main
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);
}
}
class SubClass1 extends Main {
// sub class
}
The code above generates the following result.