Hi there,In today’s article, we will discuss another important OOP(Object Oriented Programming) concept which is “Polymorphism”. I will discuss about what is Polymorphism in Java and its different types.I will try to explain in such a way that even if you want to differentiate among the types of polymorphism, it should be easier for you to do so. So, let’s get straight to the point. Poly means Many and Morph means forms so something which can take multiple forms. Easy? Let me give you a very simple example to relate to.
Just give a pause here and think what could be different forms of Water. We can warm it to make steam. We can add some other ingredients like food colour and sugar syrup to make it ice cream. Other forms of water could be gas, glaciers, Icebergs. Similarly,in Java world,when a single action is performed in multiple ways, this process is termed as “Polymorphism”.
Types of Polymorphism:
There are 2 types of Polymorphism in Java:
- Compile time Polymorphism
- Runtime Polymorphism
Compile time Polymorphism :
Compile time polymorphism is also called as Static Polymorphism.
How do we achieve Compile Time Polymorphism?
We can achieve compile time polymorphism by “Method Overloading”.
How does it work?
Internally, compile time polymorphism is handled by Compiler.
What is Method Overloading?
When two or more methods has same name.
When both the methods are in same class.
When both the methods has different arguments(No.of argument or Type of arguments or Sequence of arguments)
Now, let’s understand this by Constructor Overloading example.
In the above diagram,constructor name is Demo(). We are overloading it by using different parameters list. It is showing by using same name, same class and different type of arguments. This is an example of constructor overloading which fits perfectly in our scenario. Next, Let’s move on to understand Runtime Polymorphism.
Run time polymorphism is also called as Dynamic Polymorphism.
How do we achieve Runtime Polymorphism?
We can achieve runtime polymorphism by “Method Overriding”. When a child class or a subclass has the same method as declared in super class or parent class,then this process is called as method overriding.
How does it work?
Internally, it is handled by JVM.
What is Method Overriding?
When two or more methods has the same name.
When two or more methods are in different classes.
When methods has same arguments(No. of arguments or Type of arguments or sequence of arguments)
Let us try to understand method overriding with a simple example.
In the above diagram, Shape is a parent class which has one method named draw(). There are 3 child classes: Square class, Circle class and Hexagon class where each one of the class has one method named draw().Here, you are overriding the draw() method from parent class in child class. The method name is same in both parent and child classes and arguments are also same which is no arguments in this case.
I hope, this will help you in understanding about Polymorphism and different types of Polymorphism in Java. You can start writing with some basic code to implement polymorphism for practice purposes.
In my previous article, i discussed about “Inheritance” in Java. If you have not read it yet and interested to know about it,feel free to check it out.
Please feel free to share your inputs. Lets learn,grow and improve together. Thank you for stopping by!