Method selection
This table shows which methods will be selected when called them on a reference.
eat() | sleep() | meow() | dies() | |
Animal reference to Animal object | Animal | Animal | Not possible as method does not exist in Animal | Animal |
Cat reference to Cat object | Cat | Cat | Cat | Animal |
Animal reference to Cat object | Cat | Cat | Not possible as method does not exist in Animal | Animal |
Cat reference to Animal object. * |
* A Cat reference cannot refer to an Animal object.
Animal: eat, sleep, dies.
Cat: eat, sleep, meow.
Notes:
- The overridden method on the subclass object will be called even if the current reference is that of the parent type.
- Regarding overridden methods its the object type that determines the method called, not the reference type.
Leave a Reply