Polymorphism is one of the 4 main OOP principles. It's the ability to take many forms.
Compile time Polymorphism occurs in function overloading and operator overloading.
Example of function overloading - many functions with the same name that accept different arguments:
Result:
Runtime Polymorphism occurs in function overriding .
This means that the base class has a function that's being overwritten in the children classes:
Output:
The function calcArea
in Figure
class
is overwritten by Square
and Triangle
classes.