简体   繁体   中英

Get the interface name from the 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 possible to do that?

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

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

To get just the name

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM