"The process of representing one form in multiple forms is known as Polymorphism. OR The ability to exist in different forms is called polymorphism."
In Java, a variable, an object or a method can exist in different forms, thus performing various tasks depending on the context. Because same variable or same method can perform different tasks, the programmer has the advantage of writing flexible code. In normal words, Polymorphism Is the ability by which, We can create reference variables or functions which behaves differently in different programmatic context. When using variables, some times inherently the data type of the result is decided by the compiler and accordingle execution proceeds.
For example in the statement:
Java compiler decides the data type of the result of the expression a+b depending on the data types of a and b.
If a and b are int type, then a+b will also be taken as int type. If a and b are float type variables, then a+b will be taken as float type. If a is int and b is float, then the compiler converts a also into float and then sum is found. Thus, the result a+b is exhibiting polymorphic nature. It may exist as an int or as a float or as some other data types depending on the context. This is also called coercion.
Real life example of polymorphism Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.
There are two types of Polymorphism
1) Static Polymorphism / Compile Time Polymorphism / Method Overloading
2) Dynamic Polymorphism/ Run Time Polymorphism or Method Overriding
1) Compile time polymorphism:-
Writing two or more methods with the same name but with different signatures is called method overloading
number of arguments or data type or order of position of Parameters should be the different in method overloading
The scope of overloading is within the class.
In method overloading, method return type can be same or different.
Also known as compile time polymorphism or static polymorphism or early binding/ method overloading.
JVM decides which method is called depending on the difference in the method signature
static, Private and final methods can be overloaded.
Overloading can be done at both static and non-static methods.
Overloading can be exhibited both are method and constructor level.
Access modifiers point of view no restriction.
Arguments of method must be different at least arguments.
Method signature must be different.