java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to get super class of an object?

package com.javacoderanch.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