Polymorphism, at its core, allows for flexible code in object-oriented programming. It essentially means “having many forms” and lets objects or functions exhibit different behaviors in various contexts. It enables code to be written in a more generic and flexible manner, allowing for more modular and extensible designs
Polymorphism is mainly of two types:
1. Compile-time polymorphism (Static binding)
Since the compiler can definitively determine which function to call based on the information available at the time of compilation, it’s called static polymorphism. Static polymorphism is achieved via method overloading or operator overloading
(a) Method Overloading
We can have multiple methods with the exact same name but different signatures and the compiler is able to decide which method to call.
The method signature includes:
number of parameters
data types of respective parameters
order of the parameters
Note that the method return type is NOT a part of it’s signature
There is typically no inheritance of classes involved in overloading