List the Interfaces that an Interface Extends in Java

Java 8Object Oriented ProgrammingProgramming

The interfaces that are implemented by an interface that is represented by an object can be determined using the java.lang.Class.getInterfaces() method. This method returns an array of all the interfaces that are implemented by the interface.

A program that demonstrates this is given as follows −

Example

 Live Demo

package Test;
import java.lang.*;
import java.util.*;
public class Demo {
   public static void main(String[] args) {
      listInterfaces(java.util.List.class);
   }
   public static void listInterfaces(Class c) {
      System.out.println("The interface is: " + c.getName());
      Class[] interfaces = c.getInterfaces();
      System.out.println("The Interfaces are: " + Arrays.asList(interfaces));
   }
}

Output

The interface is: java.util.List
The Interfaces are: [interface java.util.Collection]

Now let us understand the above program.

The method listInterfaces() is called with java.util.List.class in the method main(). A code snippet which demonstrates this is as follows −

listInterfaces(java.util.List.class);

In the method listInterfaces(), the method getName() is used to print the name of the interface. Then the method getInterfaces() is used to return an array of all the interfaces that are implemented by the interface. Then this array is printed using  Arrays.asList(). A code snippet which demonstrates this is as follows −

System.out.println("The interface is: " + c.getName());
Class[] interfaces = c.getInterfaces();
System.out.println("The Interfaces are: " + Arrays.asList(interfaces));
raja
Published on 09-Jan-2019 10:38:18
Advertisements