Polymorphism in Java – Master the Concept in Just 7 Mins.

Polymorphism is a mechanism for getting different characteristics of the same instance. Didn’t get it? Let’s understand the concept of Polymorphism in Java with a real-time example.  Consider a woman can have different characteristics at the same time as she’s a mother, a wife, an employee, a daughter and so on. So the same person has different behavior in different situations.

Real-time example of Polymorphism in Java

What is Polymorphism in Java with Real-Time Example

Polymorphism is the ability to take different forms. The word “poly” means many and “morph” means forms, so it means having many forms. It is the most important concept of object-oriented programming language.

Java Polymorphism allows us to perform a single task in different ways. It is used when we have multiple classes that are related to each other by inheritance.

Do you know what is Inheritance in Java

public class Animal
{
   public void sound()
   {
      System.out.println("Animal is making a sound");   
   }
}

Now, for example, we have two subclasses of Animal: Frog and Cow that extends Animal class. We can implement the same method like this:

public class Frog extends Animal
{
   public void sound()
    {
        System.out.println("Turrrr");
    }
}

And

public class Cow extends Animal
{

    public void sound()
    {
        System.out.println("Moooooo");
    }
}

In the following example, we have a common method sound but with this method, we are using different ways to do the same action. This is a perfect example of polymorphism.

It’s the right time to explore Encapsulation in Java

Types of Polymorphism in Java

There are two types of polymorphism in Java and they are as follows:

Java Polymorphism with its types

1. Static Polymorphism in Java

A polymorphism that is resolved during compile time is known as Compile Time Polymorphism. Java Method overloading is an example of compile-time polymorphism.

Method Overloading

Method overloading in Java allows us to have more than one method with the same name but using different parameter lists. In the following example, we have two definitions of the same method addition(). So the addition method would be determined by its parameters at the compile time that’s why it is also called as Compile Time Polymorphism.

Example of Compile Time Polymorphism

package com.dataflair.polymorphism;
class Calculator
{
  int addition(int operand1, int operand2)
  {
    return operand1+operand2;
  }
  int  addition(int operand1, int operand2, int operand3)  
  {
    return operand1+operand2+operand3;
  }
}
public class CompileTimePolymorphism
{
  public static void main(String args[])
  {
    Calculator obj = new Calculator();
    System.out.println("Addition of two operands is "+obj.addition(10, 20));
    System.out.println("Addition of three operands is "+obj.addition(10, 20, 30));
  }
}

Output-Compile Time Polymorphism in java

2. Dynamic Polymorphism in Java

It is a process in which a call to an overridden method is resolved at runtime, this is the reason it is also known as runtime polymorphism in Java. One of the ways to achieve Dynamic polymorphism is method overriding in Java.

Method Overriding

In an object-oriented language, Overriding is a principle that allows a derived class or a child class to provide a specific implementation of a method that is already provided by one of its base class or parent class.

When a method in Java has derived class which has the same name, using the same parameters and same return type in its base class, then the method in the derived class overrides the method in the base class.

In the following example, we are using the calculate() method in both the Multiplication and Division class. So for the Division class, the calculate() method will divide the operands and for the Multiplication class the operands will multiply the operands.

Example of Runtime Polymorphism

package com.dataflair.polymorphism;
class Multiplication
{
  int calculate(int operand1,int operand2)
  {
    return operand1*operand2;
  }
}
class Division extends Multiplication
{
  int calculate(int operand1,int operand2)
  {
    return operand1/operand2;
  }
}
public class RunTimePolymorphism {
  public static void main(String[] args)
  {
    
    Multiplication obj=new Multiplication();
    System.out.println("Multiplication of two operands is "+obj.calculate(20, 10));
 
    Multiplication obj1=new Division();
    System.out.println("Division of two operands is "+obj1.calculate(20, 10));

  }

}

Output-Example of Runtime Polymorphism

Method Overloading vs Method Overriding

  • Method overloading is mainly used to increase the readability of a program while method overriding provides a specific implementation of a method that is already declared by its parent class.
  • Overloading is performed within the class whereas overriding occurs between two classes that have IS-A relationship.
  • In overloading, the parameter list must be different, In overriding parameter list is always the same.
  • Overloading is the example of Compile Time Polymorphism while overriding is the example of Run Time Polymorphism.

Overloading-vs-Overriding

 

Summary

Polymorphism is one of the features of OOPs. Every Java aspirant should be an expert in this concept. The two types of polymorphism – static (method overloading) and dynamic (method overriding) polymorphism, which shows their state of compilation. For a more understanding of their difference, you must visit method overloading vs method overriding in Java.

Hope you liked the explanation, please share your experience with us!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.