File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Comparing classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing classes" Watch "Comparing classes" New topic

Comparing classes

Dev Cunningham

Joined: Oct 12, 2001
Posts: 9
How do I test if one class is an 'instance of' another class when both of the operands are, in fact, classes?
I can check whether an object is an instance of a class, but in this case I have two classes -- dynamic at run time -- and I want to see if one implements the other.
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
use the isAssignableFrom(Class cls) of the Class object to check the same.
hope u gottit

Help me!Help you!!!
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Notice that assignableFrom has the *reverse logic* of instanceof! That is:
(a instanceof B) <==> (B.class.assignableFrom(a.getClass()))
[ May 20, 2002: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dev Cunningham

Joined: Oct 12, 2001
Posts: 9
I agree. Here's the link:
subject: Comparing classes
It's not a secret anymore!