Class asSubclass(Class) in Java

asSubclass(Class<U>): This method is available in the java.lang.Class of Java.

Syntax:

Class<? extends U> java.lang.Class.asSubclass(Class<U> clazz)

This method takes one argument. This method casts this Class object to represent a subclass of the class represented by the specified class object. Checks that the cast is valid, and throws a ClassCastException if it is not. If this method succeeds, it always returns a reference to this Class object.

This method 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.

Type Parameters: <U> the type to cast this Class object to.

Parameters: One parameter is required for this method.

clazz: the class of the type to cast this Class object to.

Returns: this Class object, is cast to represent a subclass of the specified class object.

Throws:

1. ClassCastException - if this Class object does not represent a subclass of the specified class (here "subclass" includes the class itself).

Approach 1: When no exception

Java

package com.Class;

public class ClassasSubclass {
    @SuppressWarnings("unchecked")
    public static void main(String[] args)
throws ClassNotFoundException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.Thread");

        @SuppressWarnings("rawtypes")
        Class clazz = Class.forName("java.lang.Thread");

        System.out.println(class1.asSubclass(clazz));
    }
}

Output:

class java.lang.Thread


Approach 2: ClassCastException

Java

package com.Class;

public class ClassasSubclass {
    @SuppressWarnings("unchecked")
    public static void main(String[] args)
throws ClassNotFoundException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.Thread");

        @SuppressWarnings("rawtypes")
        Class clazz = Class.forName("java.lang.String");

        System.out.println(class1.asSubclass(clazz));
    }
}

Output:

Exception in thread "main" java.lang.ClassCastException: class java.lang.Thread at java.base/java.lang.Class.asSubclass(Class.java:3851) at com.Class.ClassasSubclass.main(ClassasSubclass.java:13)


Some other methods of Class

arrayType()This method returns a Class for an array type whose component type is described by this Class.

asSubclass(Class)This method casts this Class object to represent a subclass of the class represented by the specified class object.

cast(Object)This method casts an object to the class or interface represented by this Class object.

componentType()This method returns the component type of this Class, if it describes an array type, or null otherwise.

describeConstable()This method returns a nominal descriptor for this instance if one can be constructed, or an empty Optional if one cannot be.

descriptorString()This method returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

desiredAssertionStatus()This method returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.

Class.forName(String)This method returns the Class object associated with the class or interfaces with the given string name.

Class.forName(String, boolean, ClassLoader)his method returns the Class object associated with the class or interfaces with the given string name, using the given class loader.

getAnnotatedInterfaces()This method returns an array of AnnotatedType objects that represent the use of types to specify super interfaces of the entity represented by this Class object.

getAnnotatedSuperclass()This method returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object.

getAnnotation(Class)This method returns this element's annotation for the specified type if such an annotation is present, else null.

getAnnotations()This method returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0.

getAnnotationsByType(Class)This method returns annotations that are associated with this element.

getCanonicalName()This method returns the canonical name of the underlying class as defined by The Java Language Specification.

getClasses()This method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

getClassLoader()This method returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader.

getComponentType()This method returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.

getConstructor(Class...)This method returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

getConstructors()This method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

getDeclaredAnnotation(Class)This method takes one argument. This method returns this element's annotation for the specified type if such an annotation is directly present, else null.

getDeclaredAnnotations()This method returns annotations that are directly present on this element. This method ignores inherited annotations. 

getDeclaredAnnotationsByType(Class)This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

getDeclaredClasses()This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

getDeclaredConstructor(Class...)This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

No comments:

Post a Comment