Polymorphism in Java
In the previous post, we have seen Initializer Block.In this post , we will see what is polymorphism in Java. In object oriented programming, we know it is best at providing the reusability of the code. Polymorphism concept also contribute in providing reusabiliy. We will see it in details.
What is the significance of name polymorphism?
Poly and morphe are 2 greek words that have meaning many and form respectively. Polymorphism is combination of these 2 words that means manyform i.e. something which can be used in many form.
Polymorphism allows us to have multiple implementation of one object or method(that may have the same name). Computers differentiates between them by the method signatures (the list of parameters: their number, their types, and the order of the types.)
There are 2 Forms of polymorphism :
- Compile time polymorphism or also called early binding
- Run time polymorphism or also called late binding
Suppose, there are 2 methods which are A() and A(int b). Now, after compiling the code into byte code compiler knows which method to execute. Since, method name is same but parameter list is different known as method Overloading and it happens at compile time, that’s why known as Compile Time Polymorphism.
Consider, there are 2 methods whose name and parameter list are same, but implementation or behavior is different i.e. A(){ //buy a book} and A(){// buy a laptop}. This is known as method overriding and as computer does’t understand which one to execute at compile time, So their binding is resolved at run time which is known as Run Time Polymorphism. Sometimes,run-time polymorphism is referred to as dynamic binding as well.
Let’s focus on Run Time Polymorphism in this post. In this process, a reference variable of superclass is created which points to child class object.It is also known as upcasting.
Understand with an example given below.
class P{} class C extends P{} P obj = new C();// upcasting
Polymorphism can be applied to methods of the class. Let’s understand with some examples.
We have one class called Machine and another child class Printer. A reference variable of superclass i.e. Machine points to the object of child class which is Printer.
class Machine { void turnsOn(){ System.out.println("Starts a machine"); } } class Printer extends Machine{ //Override void turnsOn(){ System.out.println("Starts a printer"); } public static void main(String[] args) { Machine p = new Printer(); // upcasting p.turnsOn(); // It will call the child class method } }
Can we use run time polymorphism with data-members of the class?
We will take the same classes as above and declare a boolean variable called ‘start’ which will tell the status of machine i.e. true or false.We make a reference variable of parent class which points towards the child object Printer.But it prints the output from the parent class ,that means polymorphism does not apply on data members of class.
class Machine { boolean start= false; } class Printer extends Machine{ boolean start = true; public static void main(String[] args) { Machine p = new Printer(); System.out.println("Running : "+p.start); //will print parent class data member } }
What if there is multilevel inheritance?
Let’s see how to apply polymorphism if there is multilevel inheritance involved.
package com.test; class Machine { void turnsOn(){ System.out.println("Starts a machine"); } } class Laptop extends Machine{ void turnsOn(){ System.out.println("Starts a Laptop"); } } class Desktop extends Machine{ void turnsOn(){ System.out.println("Starts a Desktop"); } public static void main(String[] args) { Machine M = new Machine(); Machine P = new Laptop(); Machine D = new Desktop(); M.turnsOn(); P.turnsOn(); D.turnsOn(); } }
Starts a machine
Starts a Laptop
Starts a Desktop