The final pillar of object-oriented programming that we’ll explore is polymorphism.
Polymorphism is an object-oriented mechanism that allows for objects of different types to be used in the same way.
We’ve already encountered polymorphism made possible by
inheritance when talking about
casting with our
In that case, we stored an object of type
HouseCat in its compatible type,
Let’s take a closer look at how polymorphism might work in our cat-centric application.
Suppose we had a
CatSitter class like the one below:
FeedTheCat uses the property
Pet, which is of type
Cat. Since a
HouseCat is a
Cat via inheritance, it is
perfectly acceptable to use an instance of
HouseCat to fill the
FeedTheCat can accept
Tiger instances as well. This
is because the only thing that the method requires is that the input
parameter has the methods defined within
Cat. Via inheritance,
Cat satisfy this requirement.
In addition to using classes to code in a polymorphic way, we can use interfaces.