Hot Posts

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: