Home » Java programming language

Java Class class getSuperClass() method with example

Class class getSuperClass() method: Here, we are going to learn about the getSuperClass() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 11, 2019

Class class getSuperClass() method

  • getSuperClass() method is available in java.lang package.
  • getSuperClass() method is used to return the Class denoting the superclass of any of the classes, interfaces, primitive type or any void type denoted by this Class.
  • getSuperClass() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getSuperClass() method does not throw an exception at the time of returning a superclass.

Syntax:

    public Class getSuperClass();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Class, it returns the super class of the entity denoted by this object.

Example:

// Java program to demonstrate the example 
// of Class getSuperClass() method of Class 

import java.util.*;

public class GetSuperClassOfClass {
    public static void main(String[] args) {
        // It returns the super class of
        // the class ChildClass

        Class cl = ChildClass.class.getSuperclass();

        if (cl != null) {
            System.out.print("Super class of ChildClass: ");
            System.out.println(cl.getName());
        } else
            System.out.println("No super class exists");
    }
}

class ChildClass extends HashSet {
    public ChildClass() {}
}

Output

Super class of ChildClass: java.util.HashSet


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.