In this tutorial, we will learn about polymorphism, different types of polymorphism and how to implement them in Java with the help of examples.
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
Polymorphism is considered as one of the important features of Object Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.
Polymorphism allows us to create consistent code. For example,
A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism.
It is also known as static polymorphism. This type of polymorphism is achieved by method overloading or operator overloading.
When there are multiple methods with same name but different parameters in same class then these methods are said to be overloaded. Methods can be overloaded by change in number of arguments or/and change in type of arguments.
// Java program for Method overloading
class MultiplyFun
{
// Method with 2 parameter
static int Multiply(int a, int b)
{
return a * b;
}
// Method with the same name but 2 double parameter
static double Multiply(double a, double b)
{
return a * b;
}
}
class CodeMistic
{
public static void main(String[] args)
{
System.out.println(MultiplyFun.Multiply(2, 4));
System.out.println(MultiplyFun.Multiply(5.5, 6.3));
}
}
Output :
8
34.65
// Java program for Method overloading
class MultiplyFun
{
// Method with 2 parameter
static int Multiply(int a, int b)
{
return a * b;
}
// Method with the same name but 3 parameter
static int Multiply(int a, int b, int c)
{
return a * b * c;
}
}
class Codemistic
{
public static void main(String[] args)
{
System.out.println(MultiplyFun.Multiply(2, 4));
System.out.println(MultiplyFun.Multiply(2, 7, 3));
}
}
Output :
8
42
Java also provide option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them. In java, Only “+” operator can be overloaded:
// Java program for Operator overloading
class OperatorOverloading
{
void operator(String str1, String str2)
{
String s = str1 + str2;
System.out.println("Concatinated String - "+ s);
}
void operator(int a, int b)
{
int c = a + b;
System.out.println("Sum = " + c);
}
}
class Main
{
public static void main(String[] args)
{
OperatorOverloading obj = new OperatorOverloading();
obj.operator(2, 3);
obj.operator("Code", "Mistic");
}
}
Output :
Sum = 5
Concatinated String - CodeMistic
It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, thats why it is called runtime polymorphism. We have already discussed method overriding in detail in a separate tutorial, refer it: Method Overriding in Java.
Example :
// Java program for Method overridding
class Parent
{
void Print()
{
System.out.println("parent class");
}
}
class child1 extends Parent
{
void Print()
{
System.out.println("child1");
}
}
class child2 extends Parent
{
void Print()
{
System.out.println("child2");
}
}
class TestPolymorphism
{
public static void main(String[] args)
{
Parent a;
a = new child1();
a.Print();
a = new child2();
a.Print();
}
}
Output :
child1
child2