How do I get super class of an object?

package org.kodejava.example.lang;

public class ObtainingSuperClass {
    public static void main(String[] args) {
        //
        // Create an instance of String class
        //
        Object o = new String("Hello");
        
        //
        // Get String class super class
        //
        Class clazz = o.getClass().getSuperclass();
        System.out.println("Super Class = " + clazz);
        
        //
        // Create an instance of StringIndexOutOfBoundsException class
        //
        o = new StringIndexOutOfBoundsException("Error message");
        
        //
        // Get StringIndexOutOfBoundsException class super class
        //
        clazz = o.getClass().getSuperclass();
        System.out.println("Super Class = " + clazz);
    }
}

The program above prints the following string:

Super Class = class java.lang.Object
Super Class = class java.lang.IndexOutOfBoundsException

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply