• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance - weird behaviour ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this piece of code:

I do not understand why line marked //1 calls the subclass's
f_() method instead of the superclass's ?
Thanks
Pho
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since f_() method is overridden in Child it will get called if the Object is Child no matter where. Becuase of late binding at runtime the method that will be called depends on the acutally Object. Since Child is in used and it has f_() this method will get called.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic