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?
The getClass(). getInterfaces() method return an array of Class that represents the interfaces implemented by an object.
Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.
Differences between a Class and an Interface:A class can be instantiated i.e, objects of a class can be created. An Interface cannot be instantiated i.e, objects cannot be created. Classes does not support multiple inheritance. Interface supports multiple inheritance.
Open Java Search, enter the interface name, click "Implementors" and you will "find which classes implement a particular interface."
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With