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.

Example: Wood has many forms table, chair, dinning table etc.

Polymorphism in java is achieved through methods.

Polymorphism example

class Shapes
{
   public void sides()
   {
      System.out.println("sides() method.");
   }
}
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)
   {
      Shapes shape = new Shapes();
      Shapes square = new Square();
      Shapes triangle = new Triangle();
      shape.sides();
      square.sides();
      triangle.sides();
   }
}


Output:

sides() method.
square has four sides.
triangle has three sides.


Types of polymorphism in java

There are two types of polymorphism in java.

  • 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.

Also read – inheritance in java

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 an 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(26, 69));
      System.out.println(obj.addition(86, 95));
   }
}


Output:

95
181


Now let’s see an 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


Operator overloading in java: In java only “+” operator can be overloaded.

“+” operator when added with numbers adds two numbers and when placed between two strings concatenates them.

Let’s see an example on operator overloading in java.

class Operator
{
   void display(String s1, String s2)
   {
      String strOutput = s1 + s2;
      System.out.println("Concatenation of two strings: " + strOutput);
   }
   void display(int num1, int num2)
   {
      int sum = num1 + num2;
      System.out.println("Sum of two numbers: " + sum);
   }
}
public class OperatorOverloading
{
   public static void main(String[] args)
   {
      Operator obj = new Operator();
      obj.display(10, 4);
      obj.display("flower", "brackets");
   }
}


Output:

Sum of two numbers: 14
Concatenation of two strings: flowerbrackets


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).

Also read – abstraction in java

Let’s see an 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