Understanding polymorphism
Polymorphism is the concept of the OOP paradigm where we can reuse the name of a function from a parent class either by redefining or overriding an existing function or by creating two different functions for two different classes with the same name and using them separately. In this section, we will look at examples for both variations of polymorphism:
- Polymorphism within inheritance
- Polymorphism in independent classes
Polymorphism within inheritance
Let us look at the earlier example of the child class, NYC
, which inherits from Branch
. To calculate the selling price along with the local tax rate for the specific branch, we created a new method within the NYC
class named calc_tax_nyc
. Instead of creating a new method, we can also override the Parent
method, calc_tax
, with the new calculation in the child class. This concept is polymorphism within inheritance. Here is a representation of it:
Figure 2.9 –...