• 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
Posts: 21154
32
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