Polymorphism in java

Let’s learn polymorphism in java.

Polymorphism in java

Polymorphism is the ability of a method to perform different tasks. It literally means having different forms.

Also read – abstraction in java

When a method is defined, its property of having many forms is called as polymorphism.


Types of polymorphism

Polymorphism in java has two types

  • Compile time polymorphism
  • Runtime polymorphism

Compile time polymorphism:

It is method overloading technique. In compile time polymorphism, class can have more than one method. Compile time polymorphism is also called as static binding.

If a class have multiple methods with same name but have different parameters is known as method overloading.

NOTE: overloading method is resolved at compile time.

Compiler is able to figure out method call at compile time that’s the reason it is known as compile time polymorphism. Let’s see an example,

public class StaticBindingDemo
{
   public static void main(String[] args)
   {
      Subtraction sub = new Subtraction();
      sub.subtract(10, 4);     // method call
      sub.subtract(25, 5, 7);  // method call
   }
}

class Subtraction
{
   void subtract(int a, int b)
   {
      System.out.println("Subtracting two integers : " + (a - b));
   }
   void subtract(int m, int n, int o)
   {
      System.out.println("Subtracting three integers : " + (m - n - o));
   }
}



Output:

Subtracting two integers : 6
Subtracting three integers : 13


Runtime polymorphism

In runtime polymorphism a method is defined in the subclass with same name and same signature as its superclass.

Runtime polymorphism is referred as method overriding. It is also called dynamic binding. Let’s see an example,

NOTE : 

  • Runtime polymorphism can be implemented only through IS-A relationship, i.e, through inheritance.
  • runtime polymorphism is invoked by JVM.
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 Brezza extends Car
{
   void accelerate()
   {
      System.out.println("brezza accelerating");
   }
}

public class RuntimePolymorphismDemo 
{
   public static void main(String[] args) 
   {
      Car obj = new Baleno();
      obj.accelerate();
      obj = new Ciaz();
      obj.accelerate();
      obj = new Brezza();
      obj.accelerate();
   }
}



Output:

polymorphism in java

Up-casting in java

Up-casting means converting subclass type to superclass type. In other words child class object is assigned to parent class reference variable. Let’s see an example,

class X
{

}
class Y extends X
{

}
X obj = new Y();


NOTE: 

  • by default upcasting happens automatically.
  • here we call class X method but in runtime it will call overridden class Y method.
  • variables are not overridden because decision takes place at compile time.
  • If Superclass method is not overridden in child class then superclass method which will be inherited to child class is called.

Let’s see a java program,

class Car
{
   int price = 500000;
   // overridden in child class
   void call()
   {
      System.out.println("car method");
   }
   // not overridden but inherited to child
   void superClassMethod()
   {
      System.out.println("super class car method");
   }
}

class Baleno extends Car
{
   int price = 600000;
   void call()
   {
      System.out.println("baleno method");
   }

   void childClassMethod()
   {
      System.out.println("child class baleno method");
   }
}

public class UpcastingDemo 
{
   public static void main(String[] args) 
   {
      Car obj = new Baleno();
      obj.call();
      obj.superClassMethod();
      System.out.println(obj.price);
   }
}



Output:

baleno method
super class car method
500000