Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance and Polymorphism Involving Interface Casting

 
Jae Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers!
Can someone please explain me how compiler is always happy with interface casting? Please look at the code below.
Any help is appreciated..!

 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 15, _A does not implement interface, but a subclass of it might implement it. Hence it is allowed.

Only when the class is final..compiler is sure it cannot have subclasses..and hence they cannot implement the interface.

In the last line of your code ..Line 28



..if you try to cast c (which is final) instead of a..you can upset the compiler then
 
Jae Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation it makes sense now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic