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 – inheritance in java

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


Types of polymorphism in java

Polymorphism in java has two types

  • Compile time polymorphism in java(static binding or method overloading)
  • Runtime polymorphism in java(dynamic binding or method overriding)

Compile time polymorphism in java (static binding or method overloading):

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

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.
  • overloading of methods is called through reference variable of a class.

It is method overloading technique. In compile time polymorphism, class can have more than one method.

Compile time polymorphism is also called as static polymorphism. Below are ways to overload method in compile time polymorphism or static polymorphism,

  1. change number of arguments
  2. change data type

Let’s see an example by changing number of arguments,

class CompileTimeDemo 
{
   void addition(int a, int b)
   {
      System.out.println(a + b);
   }
   void addition(int a, int b, int c)
   {
      System.out.println(a + b + c);
   }
   public static void main(String[] args) 
   {
      CompileTimeDemo obj = new CompileTimeDemo();
      obj.addition(25, 59, 56);
      obj.addition(63, 48);
   }
}



Output:

140
111

Now let’s see an example by changing the data type,

public class CompileTimeDemo 
{
   void addition(int a, int b)
   {
      System.out.println(a + b);
   }
   void addition(double a, double b, double c)
   {
      System.out.println(a + b + c);
   }
   public static void main(String[] args) 
   {
      CompileTimeDemo obj = new CompileTimeDemo();
      obj.addition(59.5, 85.5, 45.5);
      obj.addition(46, 53);
   }
}



Output:

190.5
99


Runtime polymorphism (dynamic binding or method overriding)

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

Upcasting in java

Up casting in java 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