Get interface name from implementation class

Example:

List<String> list = new ArrayList<String>(); //This would give me the class name for the list reference variable. list.getClass().getSimpleName(); 

I want to get the interface name from the list reference variable. Is there any way to do this?

+9
source share
2 answers

Using Reflection, you can call the Class.getInterfaces() method which returns the Array of Interfaces that your class implements.

 list.getClass().getInterfaces()[0]; 

To get only the name

 list.getClass().getInterfaces()[0].getSimpleName(); 
+13
source
 Class aClass = ... //obtain Class object. Class[] interfaces = aClass.getInterfaces(); 
+2
source

All Articles