Polymorphism in java

Let’s learn 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): compile time polymorphism is achieved through method overloading.
  • Runtime polymorphism (dynamic polymorphism): run time polymorphism is achieved through method overriding.

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.

Example of compile time polymorphism is method overloading. Below are ways to overload a method at compile time,

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

Let’s see example of 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(26, 69));
      System.out.println(obj.addition(86, 95));
   }
}


Output:

95
181


Now let’s example of 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 binding or method overriding)

Before understanding runtime polymorphism in java first let’s understand upcasting in java.

Upcasting means to convert subclass type to superclass type. Upcasting in java happens automatically and no need to call explicitly. Let’s see an example on upcasting in java.

class SuperClass
{
   void display()
   {
      System.out.println("in SuperClass display() method");
   }
}
class SubClass extends SuperClass
{
   void display()
   {
      System.out.println("in SubClass display() method");
   }
}
public class UpcastingDemo
{
   public static void main(String[] args)
   {
      SuperClass obj = new SubClass();
      obj.display();
   }
}


Output:

in SubClass display() method


In runtime polymorphism call to an overridden method is determined at runtime.

NOTE:

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

In method overriding a method should have same name, method should be in different class and should have same arguments (same number of arguments, sequence and type). Let’s see example of 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