• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

I try to explain for myself "What is an instanceof", with subclasses?

 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is giving me headache. Sometimes I think I understand and a moment later I'm asking myself were am I looking at. I had this question wrong and I spent much time on it.

The type of the variable a is A and therefore, a may point to an instance of A or any of its subclasses (B, C, D).

I will try to explain to myself:
The declared variabeles  a and b are refering to the same instance of C

So, a is refering to an instance of C, that explains that will be true
and because of C "IS-A" B explains that will als be true
But C "IS-NOT-A" D makes that   is false

Is my explanation correct? (This hard for me to understand, I'm asking than what is going on? What does the type mean in relation to the (sub) class instance here? Than I'm getting a mess in my head and mixing things.)
 
Sheriff
Posts: 28344
97
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
Looking at the output when you run that code should help you considerably, I think. So what do you see when you run it?
 
Saloon Keeper
Posts: 28399
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the risk of giving away the plot, "instanceof" is an operator, just like "+". except that it has a longer name. It's a binary operator, so like the '%" operator. it has a left-hand side and a right-hand side. The left-hand side of "instanceof" is an object. The right-hand side is the name of a class, which can either be simple ("String") or fully-qualified ("java.sql.Date"). The output of this operation will be a binary value (true/false).

One thing that might not be intuitive is that a "class" is not just literally a class, but in Java, so is an Interface.

Thus, if you say "a instanceof  Foo", then you are checking to see if "a" is either an actual instance of class Foo OR an instance of a class which has Foo as a parent class OR the class that a actually belongs to (or one of its parents) implemenents Foo.

Thus, instanceof is a very powerful operator as it allows you to check whether an object has certain properties or methods without you having to use introspection to peer into its innards. I can feed it it an object that has been down-cast to generic java.lang.Object and easily tell if I can do "Foo" things with it, regardless of whether it's a FooUp, a FooDown, a DummyFoo and know that I can't do Foo things if the object was actually a Bar.
 
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instanceof operator might be powerful, but it usually makes me suspicious. Why do you need to check whether an object can of cannot be cast to a particular type? Is there something peculiar about the inheritance of the class in question? If you find yourself using instanceof in real‑life code, start thinkging whether you have got the inheritance right.
One place, other than in cert exams, where instanceof is definitely respectable to use, even essential, is when writing an equals() method.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic