Classes and objects

Is there a nice way of finding out whether an object磗 class X is subclass of a determined class Y?. One possible solution i found was:

X.isInstance(Y.newInstance())

But i don磘 like the idea of instantiating an object for no reason. Besides I cannot instantiate an abstract class, so Y can never be an abstract class.

[336 byte] By [Damian85a] at [2007-11-27 8:27:05]
# 1
boolean isSubclass = Y.isAssignableFrom(X);
georgemca at 2007-7-12 20:16:46 > top of Java-index,Java Essentials,Java Programming...
# 2

Suppose there are two classes A and B. You have a reference a1 of an object of class A and you want to check whether the a1's class i.e. A is a subclass of B then you can use the following syntax:

a1 instanceof B

If A extends B then the above expression will return true otherwise it will return false..

Hope it helps :)

diptaPBa at 2007-7-12 20:16:46 > top of Java-index,Java Essentials,Java Programming...