Java.lang.Class.getSuperclass() Method

Advertisements


Description

The java.lang.Class.getSuperclass() 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

Example

The following example shows the usage of java.lang.Class.getSuperclass() method.

package com.tutorialspoint;

import java.lang.*;

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());
   }
}

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

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



Advertisements
Advertisements