Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Inspecting generic superclasses and interfaces in Java

Read the declared types of a direct superclass and interfaces, and see why the runtime class of an ordinary `ArrayList<String>` cannot reveal the `String` argument.

Version: Java SE 21

Syntax

public java.lang.reflect.Type getGenericSuperclass()
public java.lang.reflect.Type[] getGenericInterfaces()

Class.getGenericSuperclass() returns the direct superclass as a java.lang.reflect.Type. It returns null for Object, interfaces, primitive types, and void, and Object.class for array types.

Class.getGenericInterfaces() returns a Type[] containing the interfaces a class directly implements or an interface directly extends, in declaration order. It does not walk the hierarchy. If there are no such interfaces, including for primitives and void, the array is empty. For array types, the result contains Cloneable followed by java.io.Serializable.

Unlike getSuperclass() and getInterfaces(), which return Class objects without type arguments, the generic methods can return a ParameterizedType (as an array element for interfaces). Check with instanceof ParameterizedType before calling getActualTypeArguments(). The arguments are Type values: they can represent concrete classes, nested parameterized types, or type variables. A raw supertype such as List without arguments is represented by Class.

These methods read declaration signatures, not per-instance type arguments. Because of type erasure, new ArrayList<String>().getClass() gives the same class as ArrayList.class: its superclass is AbstractList<E>, where E is a TypeVariable, not String. A subclass that declares extends ArrayList<String>, by contrast, records String in its own signature.

Both methods can throw GenericSignatureFormatError for an invalid signature format, TypeNotPresentException for a missing referenced type, or MalformedParameterizedTypeException if a parameterized type cannot be constructed.

Examples

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;

public class GenericSupertypesDemo {
    interface Tagged<T> {}

    static final class Names extends ArrayList<String> implements Tagged<Integer> {}

    private static Type singleArgument(Type type) {
        if (type instanceof ParameterizedType parameterized) {
            Type[] arguments = parameterized.getActualTypeArguments();
            if (arguments.length == 1) {
                return arguments[0];
            }
        }
        throw new IllegalArgumentException("Expected a type with one type argument");
    }

    public static void main(String[] args) {
        Type parent = Names.class.getGenericSuperclass();
        Type tagged = Names.class.getGenericInterfaces()[0];
        Type plainParent = new ArrayList<String>().getClass().getGenericSuperclass();

        System.out.println("Names superclass argument is String: "
                + String.class.equals(singleArgument(parent)));
        System.out.println("Names interface argument is Integer: "
                + Integer.class.equals(singleArgument(tagged)));
        System.out.println("ArrayList superclass argument is a type variable: "
                + (singleArgument(plainParent) instanceof TypeVariable<?>));
    }
}

Result

Names superclass argument is String: true
Names interface argument is Integer: true
ArrayList superclass argument is a type variable: true

`Names` declares exactly one direct interface, so the example uses index `0`. The first two checks read `String` and `Integer` from the `Names` declaration; the third confirms that the ordinary `ArrayList` superclass argument is the type variable `E`.

Related lessons

  • Java · Lesson 36 · Intermediate · Version: 2

Sources