In Java, class hierarchies follow the single inheritance model, i.e. a class can have at most one superclass. The only class that does not have any superclass is java.lang.Object, while all other classes, including user-defined classes, derive (directly or indirectly) from Object. The java.lang.Class class provides a method, called getSuperclass(), to retrieve the direct superclass (i.e. the next class encountered when walking up the hierarchy) of a given class; this method returns a Class object representing the superclass of the Class object on which the method is called; if the method is called on the Object class, null is returned.

The following example demonstrates the use of the getSuperclass() method. First, we create a java.awt.Checkbox object, then retrieve its superclass (which is java.awt.Component) and finally print the name of the superclass.

import java.awt.*;

public class GetSuperclass  {
  public static void main(String[] args) {
    Checkbox myObject = new Checkbox();
    printSuperclass(myObject);
  }
  static void printName(Object object) {
    Class myClass = object.getClass();
    Class mySuperclass = myClass.getSuperclass(); 
    System.out.println(mySuperclass);
  }
}

In the above code, when mySuperClass is passed to the println() method, the toString() method of Class is invoked, which returns a string containing the fully qualified name of the class. The output of this program is reported below:

class java.awt.Component