Java Class getInterfaces() Method



Description

The Java Class getInterfaces() method determines the interfaces implemented by the class or interface represented by this object.

Declaration

Following is the declaration for java.lang.Class.getInterfaces() method

public Class<?>[] getInterfaces()

Parameters

NA

Return Value

This method returns an array of interfaces implemented by this class.

Exception

NA

Getting Interfaces of a Class Implementing an Interface Example

The following example shows the usage of java.lang.Class.getInterfaces() method. In this program, we've created an instance of ClassDemo and then using getClass() method, the class of the instance is retrieved. Using getInterfaces(), we've retrieved all interfaces implemented.

package com.tutorialspoint;

import java.util.Arrays;

interface Print {
   public void show();
}

public class ClassDemo implements Print {

   public static void main(String[] args) {
      ClassDemo c = new ClassDemo();
      show(c.getClass());
   }
 
   public static void show(Class cls) {
      System.out.println("Class = " + cls.getName());
  
      Class[] c = cls.getClasses();
      System.out.println("Classes = " + Arrays.asList(c));
    
      // returns an array of interfaces
      Class[] i = cls.getInterfaces();
      System.out.println("Interfaces = " + Arrays.asList(i));
   }
   public void show(){}
}

Output

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

Class = com.tutorialspoint.ClassDemo
Classes = []
Interfaces = [interface com.tutorialspoint.Print]

Getting Interfaces of a Thread Class Example

The following example shows the usage of java.lang.Class.getInterfaces() method. In this program, we've used Thread Class and then using getInterfaces() method, we've retrieved all interfaces.

package com.tutorialspoint;

import java.util.Arrays;

public class ClassDemo {

   public static void main(String[] args) {
      show(Thread.class);
   }
 
   public static void show(Class cls) {
      System.out.println("Class = " + cls.getName());
  
      Class[] c = cls.getClasses();
      System.out.println("Classes = " + Arrays.asList(c));
    
      // returns an array of interfaces
      Class[] i = cls.getInterfaces();
      System.out.println("Interfaces = " + Arrays.asList(i));
   }
}

Output

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

Class = java.lang.Thread
Classes = [interface java.lang.Thread$UncaughtExceptionHandler, class java.lang.Thread$State]
Interfaces = [interface java.lang.Runnable]

Getting Interfaces of a ArrayList Class Example

The following example shows the usage of java.lang.Class.getInterfaces() method. In this program, we've used Thread Class and then using getInterfaces() method, we've retrieved all interfaces.

package com.tutorialspoint;

import java.util.Arrays;

public class ClassDemo {

   public static void main(String[] args) {
      show(ArrayList.class);
   }
 
   public static void show(Class cls) {
      System.out.println("Class = " + cls.getName());
  
      Class[] c = cls.getClasses();
      System.out.println("Classes = " + Arrays.asList(c));
    
      // returns an array of interfaces
      Class[] i = cls.getInterfaces();
      System.out.println("Interfaces = " + Arrays.asList(i));
   }
}

Output

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

Class = java.util.ArrayList
Classes = []
Interfaces = [interface java.util.List, interface java.util.RandomAccess, interface java.lang.Cloneable, interface java.io.Serializable]
java_lang_class.htm
Advertisements