Retrieve Implemented Interfaces using Java Reflection API

Consider that i have some java class and i want to get all interfaces which my class implements and want to list all those interfaces.How can i retrieve information about all implemented interfaces information using Java Reflection API, have a look at below :

I have create object such as List list = new ArrayList(); and we will retrieve all implemented information using java relection API.

We already know ArrayList Heirarchy as :
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

Java Program to retrieve Implemented Interfaces :

List list = new ArrayList();
Class originalClass = list.getClass();        

//retrieve implemeted interfaces
Class[] ointerfaces = originalClass.getInterfaces();
System.out.println("\nImplemented Interfaces - ");
for (int i = 0; i < ointerfaces.length; i++) {
 if (ointerfaces[i].isInterface()) {
  System.out.println(ointerfaces[i].getName() + " is interface");
 } else {
  System.out.println(ointerfaces[i].getName() + " is Class");
 }
}
Output :
Implemented Interfaces - 
java.util.List is interface
java.util.RandomAccess is interface
java.lang.Cloneable is interface
java.io.Serializable is interface 


No comments:

Post a Comment