tags:

views:

223

answers:

2

I'd like to check if a Class object represents a subclass of other class for example

Class class1 = Class.forName("Class1");
Class class2 = Class.forName("Class2");

 if(class1.isSubClassOf(class2)) // fake methos isSubClassOf
{
  // do sth
}

How can I implement this isSubClassOf method ?

+7  A: 

Class.isAssignableFrom() provides more-or-less what you're after, although it handle interfaces also, so may need to do a bit more extra work to be sure that it's a subclass, direct or otherwise.

skaffman
A: 

instanceof operator, IIRC.

However, you should never ever do this. Add a method to the applicable class and be done with it.

Marcin
No, instanceof is for comparing instances of classes, not the classes themselves.
skaffman
If you don't have an instance, then this is likely academic. If you do, this will work.
Marcin
No, it won't. instanceof and isAssignableFrom() are profoundly different.
skaffman