Polymorphism in java

Let’s learn what is the polymorphism in java?

Polymorphism in java

Polymorphism literally means many forms. Polymorphism is the ability of a method to perform different tasks. It can be achieved through method overloading and method overriding.

For example: Wood has many forms table, chair, dinning table etc.

Polymorphism in java example

class Shapes
{
   public void sides()
   {
      System.out.println("A shape can have many sides.");
   }
}
class Square extends Shapes
{
   public void sides()
   {
      System.out.println("square has four sides.");
   }
}
class Triangle extends Shapes
{
   public void sides()
   {
      System.out.println("triangle has three sides.");
   }
}
class PolymorphismExample
{
   public static void main(String[] args)
   {
      Square obj1 = new Square();
      obj1.sides();
      Triangle obj2 = new Triangle();
      obj2.sides();
   }
}

Output:

square has four sides.
triangle has three sides.

In the above example we have created a parent class Shapes and two child classes Square and Triangle. All these classes have method sides().

sides() method show the sides of particular shape. In the above example process of showing sides of square is different from showing sides of triangle.

Therefore, sides() method behaves differently in different classes. Hence sides() method is polymorphic.


Types of polymorphism in java

There are two types of polymorphism

  • Compile time polymorphism (static polymorphism)
  • Runtime polymorphism (dynamic polymorphism)

Compile time polymorphism (static polymorphism):

Java compiler is able to handle method call at compile time hence known as compile time polymorphism also known as static polymorphism.

Compile time polymorphism is achieved through method overloading. Below are ways to overload a method,

  1. By changing number of arguments.
  2. By changing data types of arguments.

Let’s see example on compile time polymorphism by changing number of arguments.

class Demo
{
   int addition(int x, int y)
   {
      return x + y;
   }
   int addition(int x, int y, int z)
   {
      return x + y + z;
   }
}
public class CompileTimeDemo
{
   public static void main(String[] args)
   {
      Demo obj = new Demo();
      System.out.println(obj.addition(2, 3));
      System.out.println(obj.addition(6, 5, 5));
   }
}

Output:

5
16


Now let’s example on compile time polymorphism by changing data types of arguments.

class Add
{
   // method with two parameters
   static int addition(int a, int b)
   {
      return a + b;
   }
   // method with same name but three double parameters
   static double addition(double a, double b, double c)
   {
      return a + b + c;
   }
}
public class CompileTimeExample
{
   public static void main(String[] args)
   {
      System.out.println(Add.addition(5, 3));
      System.out.println(Add.addition(2.8, 5.2, 5.1));
   }
}

Output:

8
13.1


Runtime polymorphism (dynamic polymorphism)

In runtime polymorphism call to an overridden method is determined at runtime. Runtime polymorphism is implemented using method overriding.

Method overriding means to create a method in child class which already exists in parent class with same name and same arguments. Let’s see example on runtime polymorphism.

class Car
{
   void accelerate()
   {
      System.out.println("car accelerating");
   }
}
class Baleno extends Car
{
   void accelerate()
   {
      System.out.println("baleno accelerating");
   }
}
class Ciaz extends Car
{
   void accelerate()
   {
      System.out.println("ciaz accelerating");
   }
}
class RunTimePolymorphismExample
{
   public static void main(String[] args)
   {
      Car obj;
      obj = new Baleno();
      obj.accelerate();
      obj = new Ciaz();
      obj.accelerate();
   }
}

Output:

baleno accelerating
ciaz accelerating

NOTE:

  • Runtime polymorphism is implemented only through IS-A relationship, i.e, through inheritance.
  • Runtime polymorphism is handled by JVM.