• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generalized solution to instanceof

 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic