There is always a place in the inheritance tree where they "match", because all classes ultimately inherit from Object. But your method, being named "isInstance", looks like it should be returning a boolean value. If that's the case then it would always return true.
But no doubt I'm misunderstanding your question.
Edit: I see that Class has an isInstance() method that looks a lot like the method in your question. So that would beI will leave you to read the documentation and see if it's what you meant. [ September 13, 2007: Message edited by: Paul Clapham ]
Originally posted by Paul Clapham: I see that Class has an isInstance() method
Thanks, that is clearly the tool to build what I need. What I'm looking for is roughly a lowest common superclass. Sure, everything is at the top an Object.
What I'm looking for is the lowest common class that the two objects share.
Consider a Fruit class, and many subclasses, class Berry extends Fruit
Naturally, in the real implementation, there are many levels of inheritance. then you have code like:
You want to be able to getCommonSuper(f1, f2) and get Fruit, while getCommonSuper(f1, f3) is Berry
Thanks, that is clearly the tool to build what I need. What I'm looking for is roughly a lowest common superclass. Sure, everything is at the top an Object.
I did this a while back. It is interesting, but it may not be that useful -- as sometimes the best lowest common class is actually an interface. But here it is...
Henry [ September 14, 2007: Message edited by: Henry Wong ]
as sometimes the best lowest common class is actually an interface. But here it is...
Thanks, I'm not sure this will do what I'm looking for, but its a hint. In this particular domain, its really instances of some flavor of fruit, but you are correct that the obvious implementation won't work well when its really an interface.
Nicholas Jordan
Ranch Hand
Joined: Sep 17, 2006
Posts: 1282
posted
0
[Scott Meyers:]Anytime you find yourself writing code of the form " if the object is of type T1, then do something, but if it's of type T2, then do something else, slap yourself."
How deep is the decision tree ? It appears to me Class.isInstance(Object obj) will not support a search for commonality beyond one level deep.
I have spent enough time on all-night hair-pullers that it appears to me that you are trying to do a typing or type-resolution that would be better approached with fast editors and eager fingers than with elegance.
I realize I have used a feisty approach, and may be speaking beyond my skills ... it's just that I have too much experience wrestling with this and type resolution seems to me to be better approached by other design paradigm.