Java Class getSuperclass() Method



Description

The Java Class getSuperclass() method returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.

Declaration

Following is the declaration for java.lang.Class.getSuperclass() method

public Class<? super T> getSuperclass()

Parameters

NA

Return Value

This method returns the superclass of the class represented by this object.

Exception

NA

Getting Super Class of a Class Example

The following example shows the usage of java.lang.Class.getSuperclass() method. In this program, we've created three classes where superClass is parent of subClass. Then we've created an instance of both the and then using getClass() method, the class of the instance is retrieved. Using getSuperclass(), we've retrieved the superclass and then printed its name.

package com.tutorialspoint;

class superClass {
   // super class
}

class subClass extends superClass {
   // sub class
}

public class ClassDemo {

   public static void main(String args[]) {

      superClass val1 = new superClass();
      subClass val2 = new subClass();
      Class cls;

      cls = val1.getClass(); 
      System.out.println("val1 is object of type = " + cls.getName());

      /* returns the superclass of the class(superClass) represented 
         by this object */
      cls = cls.getSuperclass();
      System.out.println("super class of val1 = " + cls.getName());

      cls = val2.getClass(); 
      System.out.println("val2 is object of type = " + cls.getName());
     
      /* returns the superclass of the class(subClass) represented
         by this object */
      cls = cls.getSuperclass();
      System.out.println("super class of val2 = " + cls.getName());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

val1 is object of type = com.tutorialspoint.superClass
super class of val1 = java.lang.Object
val2 is object of type = com.tutorialspoint.subClass
super class of val2 = com.tutorialspoint.superClass

Getting Super Class of an ArrayList Example

The following example shows the usage of java.lang.Class.getSuperclass() method. In this program, we've used class of ArrayList. Using getSuperclass(), we've retrieved the superclass and then printed its name.

package com.tutorialspoint;

import java.util.ArrayList;

public class ClassDemo {

   public static void main(String args[]) {

      Class cls = ArrayList.class;
      System.out.println("clas is object of type = " + cls.getName());

      /* returns the superclass of the class(superClass) represented 
         by this object */
      cls = cls.getSuperclass();
      System.out.println("super class of cls = " + cls.getName());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

clas is object of type = java.util.ArrayList
super class of cls = java.util.AbstractList

Getting Super Class of a Thread Example

The following example shows the usage of java.lang.Class.getSuperclass() method. In this program, we've used class of Thread. Using getSuperclass(), we've retrieved the superclass and then printed its name.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String args[]) {

      Class cls = Thread.class;
      System.out.println("clas is object of type = " + cls.getName());

      /* returns the superclass of the class(superClass) represented 
         by this object */
      cls = cls.getSuperclass();
      System.out.println("super class of cls = " + cls.getName());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

clas is object of type = java.lang.Thread
super class of cls = java.lang.Object
java_lang_class.htm
Advertisements