This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need explain instanceof operator issue  RSS feed

 
matej spac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I do not understand why instanceof operator is working as it is working. For example:


cause compilation error - this is clear, there is no IS-A relationship, Dog never ever could be a Cat.
But another example behaviour is weird for me:


works fine. It compiles and also run (and produce false of cource).
So tell me, in which universe Robot could be an Animal?
Why this don't cause compile error?

 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could be a subclass of Robot which implements Animal. The compiler sees a variable of type Robot; this could be a reference to an instance of that subclass.

Try declaring the Robot class as "final" and see what changes.
 
matej spac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow, so briefly and so clearly :-)
thanks, now I understand
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!