Close

Java Reflection - Class.getSuperclass() Examples

Code Snippets Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public Class<? super T> getSuperclass()
Returns:
the superclass of the class represented by this class object.

Examples


package com.logicbig.example.clazz;

public class GetSuperclassExample {

public static void main(String... args) {
Class<MyClass> c = MyClass.class;
Class<? super MyClass> superclass = c.getSuperclass();
System.out.println(superclass);
System.out.println(superclass.getSimpleName());
}

private static class MySuperClass {}

private static class MyClass extends MySuperClass {}
}

Output

class com.logicbig.example.clazz.GetSuperclassExample$MySuperClass
MySuperClass




package com.logicbig.example.clazz;

public class GetSuperclassExample2 {

public static void main(String... args) {
System.out.println("-- super class of an interface --");
Class<MyInterface> c = MyInterface.class;
Class<? super MyInterface> superclass = c.getSuperclass();
System.out.println(superclass);
}

private static interface MySuperInterface {}

private static interface MyInterface extends MySuperInterface {}
}

Output

-- super class of an interface --
null




package com.logicbig.example.clazz;

public class GetSuperclassExample3 {

public static void main(String... args) {
System.out.println("-- super class of Object --");
Class<Object> c = Object.class;
System.out.println(c.getSuperclass());

System.out.println("-- super class of primitive --");
Class<Integer> c2 = int.class;
System.out.println(c2.getSuperclass());

System.out.println("-- super class of primitive array --");
Class<int[]> c3 = int[].class;
System.out.println(c3.getSuperclass());

System.out.println("-- super class of non-primitive array --");
Class<Integer[]> c4 = Integer[].class;
System.out.println(c4.getSuperclass());

System.out.println("-- super class of void --");
Class<Void> c5 = void.class;
System.out.println(c5.getSuperclass());
}
}

Output

-- super class of Object --
null
-- super class of primitive --
null
-- super class of primitive array --
class java.lang.Object
-- super class of non-primitive array --
class java.lang.Object
-- super class of void --
null




See Also