What is Polymorphism in OOP

Consider the Greek roots of the polymorphism term; it will help to understand the concept.

Poly = many: Ex: polygon = many-sided. Morph = change or form: morphology = study of biological form, Morpheus = the Greek god of dreams able to take many form. So in programming, the polymorphism is the ability to present the same method or function for differing underlying forms (data types).

In other words it means one method with multiple implementations, for a certain class of action. And which implementation to be used is decided at runtime depending upon the situation (i.e., data type of the object).

For example: you are developing a calculator application, you will need to define different functions for different arithmetic operations like addition, subtraction, multiplication, division, etc. If you are defining a function for addition like.

int addition(int a, int b) { return a+b; }

But the above function can perform addition of integer type only. For example:

addition(10, 20); is valid, but addition(30.10, 15.20);

is not valid because the above addition function can accept only integer type value and not double type. To fix this issue you need to overload the addition() function for each data type.

Therefore your class will look like

class AdditionCls
{
int addition(int a, int b)
{
return a+b;
}

float addition(float a, float b)
{
return a+b;
}

double addition(double a, double b)
{
return a+b;
}

long addition(long a, long b)
{
return a+b;
}

int addition(int a, int b)
{
return a+b;
}

}