Home » Java programming language

Java Class class getGenericInterfaces() method with example

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

Class class getGenericInterfaces() method

  • getGenericInterfaces() method is available in java.lang package.
  • getGenericInterfaces() method is used to return an array denoting the interfaces implemented by the class or an interface denoted by this object.
  • getGenericInterfaces() 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.
  • getGenericInterfaces() method may throw an exception at the time of returning an array of interfaces.
    • 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 parent interfaces refer to a non-existent type.
    • MalformedParameterizedTypeException: This exception may throw when any generic parent interfaces refer to a parameterized type that cannot be initialized.

Syntax:

    public Type[] getGenericInterfaces();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Type[], it returns an array of interfaces implemented by the class.

Example:

// Java program to demonstrate the example 
// of Type[]  getGenericInterfaces () method of Class 

import java.lang.reflect.*;

public class GetGenericInterfacesOfClass {
 public static void main(String[] args) {
  StringBuilder sb = new StringBuilder();
  Class cl = sb.getClass();

  // It returns an array of interfaces objects that is
  // implemented by the Class

  Type[] ty = cl.getGenericInterfaces();

  if (ty.length != 0)
   for (int i = 0; i < ty.length; ++i) {
    System.out.print("Interfaces implemented by StringBuilder: ");
    System.out.println(ty[i].toString());
   }
  else
   System.out.println("No interface implemented by StringBuilder ");
 }
}

Output

Interfaces implemented by StringBuilder: interface java.io.Serializable
Interfaces implemented by StringBuilder: java.lang.Comparable<java.lang.StringBuilder>
Interfaces implemented by StringBuilder: interface java.lang.CharSequence


Comments and Discussions!

Load comments ↻





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