here we go,
I have an marker interface :
interface IPOJO {
}
And then i have a class implementing this interface :
class MePOJO implements IPOJO {
}
Now suppose i have a class object holding reference of class MePOJO :
Class<MePOJO> classObj = MePOJO.class;
So how can i determine if MePOJO class implements IPOJO just by using classObj ?
You should use Class#isAssignableFrom(Class<?> cls) method here:
Class<?> classObj = MePOJO.class;
System.out.println(IPOJO.class.isAssignableFrom(classObj));
Output:
true
There are several approaches for this.
Either by using the Class type of the object you've created and retrieve the list with all the interfaces that it implements.
MePOJO test = new MePOJO();
Class[] interfaces = test.getClass().getInterfaces();
for (Class c : interfaces) {
if ("IPOJO".equals(c.getSimpleName()) {
System.out.println("test implements IPOJO");
}
}
Or using the Class#isAssignableFrom(Class clazz) method:
Class<?> clazz = MePOJO.class;
System.out.println(IPOJO.class.isAssignableFrom(clazz));
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