This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing classes" Watch "Comparing classes" New topic
Author

Comparing classes

Dev Cunningham
Greenhorn

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.
Thanks!
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
author
Sheriff

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
Greenhorn

Joined: Oct 12, 2001
Posts: 9
Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Comparing classes
 
Similar Threads
Refering to another class's instance of a different class, inside a class
Regarding Local Inner Classes
Action Listeners
utility of singleton classes
Static classes and Classloaders