Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Java Polymorphism

Java Polymorphism

May 10th, 2019

Java Polymorphism

Java polymorphism is an idea of performing one action in many ways. Two Greek words “poly” meaning many and “morph” meaning forms combine together to make polymorphism.

Java supports two kinds of polymorphism:

  1. Compile time polymorphism
  2. Runtime polymorphism

Polymorphism in Java is accomplished by method overloading and overriding.

Method Overloading

Method overloading comes into picture when a class has various methods with same name yet different parameter. For example you could create a method to multiply two integers and three integers. In this case, you could have same method name but parameters will be different in both cases. For first method, signature could be multiply (int, int) and for second method, signature could be multiply (int, int, int). Based on number of parameters passed, that specific method would be called. This is how Java accomplishes compile time polymorphism. Method overloading could be achieved by either changing number of arguments or changing data type.

Method Overriding

Method overriding allows a child class to implement a method of parent class. In other words, method in parent class and child class has the same name, same parameters and overall same signature. This is how Java achieves Run time polymorphism. Since the both the methods come with same signature and name, which method will be executed is decided by the object that is used to call it. If child class’s object is used to call it, method of child class will be executed. If parent class’s object has been used to call it, method of parent class is run.

Compile time Polymorphism:

This is also called static method dispatch. In this type of polymorphism which overloaded method should be called is decided at compiled time. It is used for method overloading.  Since the response to method is known at compile time, it is called compile time polymorphism. It is also called early binding because of early awareness of compiler about the methods having same name and knowing which one to call.

Example of compile time polymorphism: Let us take the early example of multiplying two and three numbers. So here we will have two definitions of multiply method and which method will be called is decided by the parameters passed at the compile time.

class CTP
{
int multiply(int first, int second)
{
return first*second;
}
int multiply(int first, int second, int third)
{
return first*second*third
}
}
public static void main(String args[])
{
CTP object1= new CTP();
System.out.println(object1.multiply(1,2));
System.out.println(object1.multiply(1,2,3));
}

This example represents method overloading, where a method with same name has been used twice with different parameters. In this example, when multiply method is called with two arguments passed i.e. 1 and 2, int multiply (int first, int second) is called. When the same function is called with three arguments passed, int multiply (int first, int second, int third) is called. Since which method should be called has been decided before execution, at compile time, it is called compile time polymorphism and early binding.

Runtime Polymorphism:

This is also called dynamic method dispatch. In this type of polymorphism which overridden method should be called is decided at run time. It is used for method overriding.  Since the response to method is known only at run time, it is called run time polymorphism. It is also called late binding because of late knowledge about the methods having same name and knowing which one to call.

Example of run time polymorphism: Let us take the example of a method used to print a message in parent and child class. So here we will have two definitions of message method, one in parent class and another in child class and which method will be called is decided by the object used to call the method at run time.

class RTP
{
void message()
{
System.out.println(“Message from parent class:RTP”);
}
}
class childRTP extends RTP
{
void message()
{
System.out.println(“Message from child class:childRTP”);
}
}
public static void main(String args[])
{
RTP object1=new RTP();
childRTP objcet2=new childRTP();
object1.message();
object2.message();
}

Example:

when object1 calls the method message output will be:

“Message from parent class:RTP”
Because object1 is object of parent class. And object2 is object of child class, so when object2 calls the method message, Output will be,
“Message from parent class:RTP”
Apart from that, you can also make use of reference variable to invoke the method. If you create a reference of type parent class, it will initially it points to null and afterwards you can assign it to various objects and decide the invocation.
Let us work with reference of type parent class RTP.
public static void main(String args[])
{
RTP object1=new RTP();
childRTP objcet2=new childRTP();
RTP reference_variable;
reference_variable=object1;
reference_variable.message();
reference_variable=object2;
reference_variable.message();
}