Polymorphism in Java with examples

In this tutorial, we are going to learn polymorphism in Java. Polymorphism is one feature of object-oriented programming language (JAVA).

The word polymorphism is made up of two words, “poly” means many and “morphs” means forms.

In Java polymorphism means, the ability of an object to take multiple forms.

There are two types of polymorphism in Java:

  1. Compile-time Polymorphism
  2. Rum time Polymorphism

Compile-time Polymorphism

The compile-time polymorphism is achieved by method overloading or operator overloading. Therefore it is also called Static polymorphism.

Now let us understand what is Method overloading. In method overloading, we define more than one method with the same name but different type signature.

Type signature includes several parameters, type of parameters, a sequence of parameters.

Example of Compile time polymorphism:

import java.io.*;
import java.lang.*;
class ComileTimePolymorphism
{	
  public void add(int a,int b)
  {
    System.out.println("I am in add method ,where 2 int arguments are used");
    System.out.println(a+b);
  }
  public void add(int a,double b)
  {
    System.out.println("I am in add method ,where 1 int and 1 double arguments are used");
    System.out.println(a+b);
  }
  public void add(double a,double b)
  {
    System.out.println("I am in add method ,where 2 double arguments are used");
    System.out.println(a+b);
  }

}
class ComileTimePolymorphismMain
{
  public static void main(String args[])
  {
    ComileTimePolymorphism ctobj=new ComileTimePolymorphism();
    ctobj.add(2,5);
    ctobj.add(2,5.0);
    ctobj.add(2.0,5.0);
  }
}

Output:

I am in add method ,where 2 int arguments are used
7
I am in add method ,where 1 int and 1 double arguments are used
7.0
I am in add method ,where 2 double arguments are used
7.0

In the above example, we have seen that there are 3 add methods present in the same class. Out of these 3 methods which method is going to execute is decided by Compile at compile-time only. Hence the name is compile-time polymorphism. In the above code, the compiler observes the type signature of all 3 methods to decide which method will be invoked or called

Note:

For method overloading,

  1. The method can be defined as a member of the same class.
  2. It does not require inheritance

For Compile time polymorphism,

  1. The compiler is responsible for deciding which method will be called.

Run time polymorphism

The run time polymorphism is achieved by method overriding. Therefore it is also called Dynamic polymorphism.

Now let us understand what is Method overriding. In method overriding, we define more than one method with the same name as well as the same type signature.

Method signature includes several parameters, type of parameters, a sequence of parameters.

Example of run time polymorphism

import java.io.*;
import java.lang.*;
class Bank
{
  public void getInterest( double i)
  {
    System.out.println("I am in Bank class");
    System.out.println("I am in Bank class having Interest"+i);
  }

}
class Sbi extends Bank
{
  public void getInterest( double interest)
  {
    System.out.println("I am in Sbi class");
    System.out.println("I override the method of Bank(super class)");
    System.out.println("My Interest ="+interest);
  }

}
class Axis extends Sbi
{
  public void getInterest(double i)
  {
    System.out.println("I am in Axis class");
    System.out.println("I override the method of Sbi(superclass of Axis)");
    System.out.println("My Interest ="+i);
  }

}
class RunTimePolymorphism
{
  public static void main(String args[])
  {
    Bank bobj;
    
    bobj=new Sbi();
    bobj.getInterest(3.5);
    
    bobj=new Axis();
    bobj.getInterest(4.5);
  }
}

output:

I am in Sbi class
I override the method of Bank(super class)
My Interest =3.5
I am in Axis class
I override the method of Sbi(superclass of Axis)
My Interest =4.5

In the above example, As we have seen the same method is present in 2 subclasses(Sbi, Axis). At runtime the compiler decides which function to invoke hence the name is Runtime polymorphism. Overriding methods are present in SBi and Axis bank subclass. An overridden method is present in Bank (Superclass)
In the above code, the compiler observes the object name for deciding which method will be invoked or call.

Note:

For method overriding,

  1. The one method can be a member of the superclass and pother in a subclass.
  2. It must require inheritance

For Run time polymorphism,

  1. The interpreter is responsible for deciding which method will be executed for a particular method call

You can also check out my other posts at:
https://www.codespeedy.com/author/harshad_lukare/

Leave a Reply

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