More polymorphism
We already know that polymorphism means many forms, but what does it mean to us?
Boiled down to its simplest, it means the following:
Note
Any subclass can be used as part of code that uses the super class.
This means that we can write code that is easier to understand, and simpler to change.
Also, we can write code for the super class and rely on the fact that no matter how many times it is subclassed, the code will still work within certain parameters. Let's discuss an example.
Suppose that we want to use polymorphism to help write a zoo management app. We will probably want to have a function, such as feed
. Let's also say we have Lion
, Tiger,
and Camel
classes, which all inherit from a parent class called Animal
. We will also probably want to pass a reference to the animal to be fed into the feed
function. This might seem like we need to write a feed function for each and every type of Animal
.
Instead, however, we can write polymorphic functions with polymorphic arguments...