dsa

Java Polymorphism

Introduction :

In this tutorial, we will learn about polymorphism, different types of polymorphism and how to implement them in Java with the help of examples.

What is Polymorphism ?

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.

Why Polymorphism ?

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.

Types of Polymorphism :

  1. Compile-time Polymorphism
  2. Run-time Polymorphism

Compile-time Polymorphism

It is also known as static polymorphism. This type of polymorphism is achieved by method overloading or operator overloading.

  • Operator Overloading:

    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:

    1. To add integers
    2. To concatenate strings
    Example :
    
    // 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
    
  • Runtime Polymorphism

    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