The word Polymorphism is the combination of two individual words that is Poly and Morphism.
Poly means Many, and Morphism signify Forms. Generally speaking, if anything which takes multiple forms is known as Polymorphism.
It is one of the pillars of Object-Oriented Programming apart from Inheritance, Encapsulation, and Abstraction.
In Java, the method or function which takes multiple forms is called Polymorphism.
Here Multiple forms mean when in a class more than one method having the same name but accept the value of different datatype when it gets executed.
In the above, you can see I have created a class named Website, inside that two methods with the same name are present but datatype of parameter accepting both are completely different, so we can say it one kind of Method Overloaded Example.
When we try to call the method which String and int value, the first method of Website class gets executed. However, when trying to call bypassing the long and int as shown in the figure, the second method got executed.
Hence, we can say that method having the same name but behaving differently based on the data type of the argument we pass.
Multiple forms also refer to the ability of the method to behave differently based on the object it is acting upon.
In the above figure, we are calling the same method on object reference of LivingBeing class but the method that gets executed completely depends on the object on the object reference pointing.
Don’t worry I will explain it below in detail.
Types of Polymorphism
- Compile Time Polymorphism (Static Polymorphism)
- Run Time Polymorphism (Dynamic Polymorphism)
1. Compile Time Polymorphism
In the case of method overloading, when we call the overloaded method, then the decision about method binding is taken by the compiler,
It means when we call an overloaded method then the decision about which method implementation has to be executed is taken by the compiler based on the Arguments we are passing while calling that method. Please refer to the below figure for better understanding.
Since the decision is taken by the compiler, we also called it early binding.
In Java, Compile Time Polymorphism can be achieved with the help of Method Overloading.
How Compile Time Polymorphism can be achieved with Method Overloading??
Since method overloading refers to a class having more than one method with the same name but a change in signature such as a change in the number of parameters that the method accepts.
As I mentioned in the definition of Compile Time Polymorphism, the decision of which method (having the same name) will get executed is taken by the compiler based on the number of arguments we pass while calling it.
Have you noticed above, to perform compile-time polymorphism, there must be more than one method with the same name, and having the capability of accepting different numbers of parameters should present in class. That is nothing but method Overloading.
Important points About Compile Time Polymorphism
Method Overloading required to achieve Compile Time Polymorphism.
Compile-time polymorphism also refers to Static Polymorphism as well as Early Binding.
2. Run Time Polymorphism
The method can behave differently based on the Object it is acting upon.
What does the above line mean?
It means that when more than one class having the same method name but definition or logic written inside the method block is varying according to the class.
So when you call the method on object reference of Object, then it depends upon on the Object of respective Class that which method implementation or logic will get executed. Please refer to the below figure for better understanding.
In Java, Run Time Polymorphism can be achieved by using Method Overriding.
How Run Time Polymorphism can be achieved using Method Overriding??
In method Overriding we override the method of the other class so-called superclass into the other class i.e child class.
Method Override means we simply change the implementation or logic of the inherited method from the superclass.
When we try to call the method on the object reference of the superclass then the method present inside the child class
will get executed.
In the above, you can see that method execution is completely depends on the object on which object reference is pointing. Hence we can say that
Run Time Polymorphism can be achieved by using the Method Overriding. Please refer to the following figure.
In the above LivingBeing is a Superclass contains a method named eat(), This class is inherited by two child class that is Cow and Dog. Since both classes inherited then eat() will also get inherited to both the class.
And both classes we change the logic or the code written inside the eat() method, that is nothing but it is called method overriding.
I have created the object of both classes Cow as well as Dog. Both objects having the reference of Super Class that LivingBeing.
So when we call the method from the object reference which pointing to the Cow, then the method present inside the cow class will get executed.
Although when an object reference is pointing to the Dog Object then eat() method of class Dog will get executed.
Run Time Polymorphism is also called Dynamic Polymorphism.
Why Run Time Polymorphism also called Dynamic Polymorphism?
Since the method execution is completely dependent upon the object on which it is acting or calling, and object is created dynamically by JVM at Run-Time, Hence it is called Dynamic Polymorphism.
Important Point About Run Time Polymorphism
- The capability of the method to act differently based on the object.
- Run Time Polymorphism is also called Dynamic Polymorphism.
- With the help of Method Overriding, we can achieve Run Time Polymorphism.
Above there are some important terms that are frequently used is Method Overloading. I made a separate article for that, please check out if you want to learn more about that.
I hope you understand Polymorphism in a better way. If You have any queries or doubts, please feel free to ask.