aspose file tools*
The moose likes Java in General and the fly likes Generalized solution to instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generalized solution to instanceof" Watch "Generalized solution to instanceof" New topic
Author

Generalized solution to instanceof

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

The Java instanceof operator is very handly, but it requires a constant Class as the second operand, i.e.


Is there a clean way to generalize this so I could something like


with it properly going up the inheritance tree for both 'foo' and 'bas' up to where they match?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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 ]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted 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
[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.

http://en.wikipedia.org/wiki/Paradigm gives this discussion:

  • what is to be observed and scrutinized,
  • the kind of questions that are supposed to be asked and probed for answers in relation to this subject,
  • how these questions are to be structured,
  • how the results of scientific investigations should be interpreted.

  • Which I find helpful in thinking about your question.

    Browser Security
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Generalized solution to instanceof