This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need explain instanceof operator issue

 
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
Pie
Posts: 20750
30
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