Home » Java programming language

Java Class class getGenericSuperclass() method with example

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

Class class getGenericSuperclass() method

  • getGenericSuperclass() method is available in java.lang package.
  • getGenericSuperclass() method is used to return the Type denoting the generic superclass of the class or an interface or primitive type or void denoted by this Class directly.
  • getGenericSuperclass() 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.
  • getGenericSuperclass() method may throw an exception at the time of returning a generic superclass.
    • GenericSignatureFormatError: This exception may raise when the generic class signature does not match the format given in the JVM Specification.
    • TypeNotPresentException: This exception may raise when any generic superclass refers to a non-existent type.
    • MalformedParameterizedTypeException: This exception may raise when any generic superclass refers to a parameterized type that cannot be initialized at any cost.

Syntax:

    public Type getGenericSuperclass();

Parameter(s):

  • It does not accept any parameter.

Return value:

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

Example:

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

import java.lang.reflect.*;
import java.util.*;

public class GetGenericSuperClassOfClass {
 public static void main(String[] args) {
  // It returns the generic super class of
  // the class GenericClass

  Type ty = GenericClass.class.getGenericSuperclass();

  if (ty != null) {
   System.out.print("Generic Super class of GenericClass: ");
   System.out.println(ty);
  } else
   System.out.println("No super class exists");
 }
}

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

Output

Generic Super class of GenericClass: class java.util.HashSet


Comments and Discussions!

Load comments ↻





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