2

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 ?

VishalDevgire
  • 4,232
  • 10
  • 33
  • 59

6 Answers6

8

You should use Class#isAssignableFrom(Class<?> cls) method here:

Class<?> classObj = MePOJO.class;
System.out.println(IPOJO.class.isAssignableFrom(classObj));

Output:

true
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
5

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));
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • This is the right answer, but for the sake of completeness, [reflection](http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getInterfaces()) – rocketboy Aug 02 '13 at 14:38
  • Note that `OP` is having `Class>` object, and not an instance of `MePOJO`, so this isn't right answer at all. – Rohit Jain Aug 02 '13 at 14:39
  • I've updated my answer and summarized several possible approaches. – Konstantin Yovkov Aug 02 '13 at 14:49
  • I believe you should remove the first option since OP does not specify if MePOJO class has a default constructor or not. In the second option creating a new instance is not required. You can just do `MePOJO.class.getInterfaces();` – c.s. Aug 02 '13 at 14:58
  • Careful with `getInterfaces()` it does different things than `instanceof`: http://ideone.com/nAmEyO – zapl Aug 02 '13 at 15:16
3

The correct way to do this is not with marker interfaces at all but with annotations:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface Pojo {
}

@Pojo
public static class MePojo {
}

public static void main(String[] args) throws Exception {
    System.out.println(MePojo.class.isAnnotationPresent(Pojo.class));
}

Output:

true

This way you don't pollute the namespace with marker interfaces.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Depends on what you intend to do with those markings. If you want to limit methods that could take any `Object` to take only marked ones it's easier to let them implement an interface since you get compile time errors instead of having to check at runtime whether they have an annotation. `Serializable` for example. – zapl Aug 02 '13 at 14:56
  • I don't think that's a marker interface then because inheritance applies. You only want to accept `Foo`. A marker interface is, by definition, outside of inheritance. – Boris the Spider Aug 02 '13 at 14:57
  • Hmm, if you define it that way, +1 – zapl Aug 02 '13 at 15:18
2

Check with the keyword instanceOf

if(classObj  instanceOf IPOJO ){

}

The instanceof operator evaluates to true if and only if the runtime type of the object is assignment compatible with the class or interface.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

There are also class members that allow you to get the names of the super class of the object. http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Craig
  • 94
  • 6
1

Use method isInstance from Class :

if (classObj.isInstance(myObj)){
     ...
}

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html

Graham Griffiths
  • 2,196
  • 1
  • 12
  • 15