Polymorphism in java

Here we are going to 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 defined, its property of having many forms is called as polymorphism. Let’s see an example,

// Base Class
public class Animal
{ 
   public void sound()
   {
      System.out.println("sound of animals");
   }
}

// Derived Class
public class Lion extends Animal
{
   public void sound()
   {
      System.out.println("lion roars");
   }
}

// Derived Class
public class Cat extends Animal
{
   public void sound()
   {
      System.out.println("cat meows");
   }
}

public class PolymorphismExample
{
   public static void main(String[] args)
   {
      Animal animal = new Animal(); 
      Animal lion = new Lion();
      Animal cat = new Cat();
      animal.sound();
      lion.sound();
      cat.sound();
   }
}

Output:

sound of animals
lion roars
cat meows


Types of polymorphism

Polymorphism in java has two types

  • Compile time polymorphism
  • Runtime polymorphism

Compile time polymorphism:

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

In compile time polymorphism method is defined numerous times with same name but with different signature. It can be implemented in a single class.

NOTE: compile time polymorphism is handled by compiler.

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.
  • run time polymorphism is handled by JVM.
class Company
{ 
   float stockPrice()
   {
      return 0;
   } 
}

class Accenture extends Company
{ 
   float stockPrice()
   {
      return 183.90f;
   } 
}

class Infosys extends Company
{ 
   float stockPrice()
   {
      return 749.70f;
   } 
}

class IBM extends Company
{ 
   float stockPrice()
   {
      return 134.95f;
   } 
}

public class RuntimePolymorphismDemo 
{
   public static void main(String[] args) 
   {
      Company company = new Accenture(); 
      System.out.println("Accenture stock price : " + company.stockPrice()); 
      company = new Infosys(); 
      System.out.println("Infosys stock price : " + company.stockPrice()); 
      company = new IBM(); 
      System.out.println("IBM stock price : " + company.stockPrice());
   }
}

Output:

polymorphism in java

Related Posts