• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding Interface with instanceof operator

 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Nico Van Brandt
Ranch Hand
Posts: 70
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instanceof checks for an 'IS-A' relationship. For that reason, because SpeedBoat is a Toy, it returns true.
 
Hakim Benhamadouche
Greenhorn
Posts: 5
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd add that parent classes and interfaces are part of the inheritance tree of a class therefore the 'IS-A' principle is applied, as said previously'.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though at that time b is a SpeedBoat and therefore a Toy, the reference is used to determine which methods can be called, and the reference is one to Boat, not Toy or SpeedBoat. The compiler doesn't look at the actual value of the reference.
 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your replies
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic