Introduction

Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways. For example, lets say we have a class Animal that has a method sound().

public class Animal{
  
  public void sound(){
    System.out.println("Animal is making a sound");   
  }
}

public class Horse extends Animal{

  @Override
  public void sound(){
    System.out.println("Neigh");
  }
}

 

Method sound() is the common action for all subclasses but implemented in different ways to do the same action. This is a perfect example of polymorphism (feature that allows us to perform a single action in different ways).

There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

 

Compile Time Polymorphism

As the meaning is implicit, this is used to write the program in such a way, that flow of control is decided in compile time itself. It is achieved using method overloading.

In method overloading, an object can have two or more methods with same name, but with their method parameters different. These parameters may be different on two bases:

class StaticPolymorphism{

  // Method 1
  public int add(int x, int y){
    return x+y;
  }

  // Method 2
  public int add(int x, int y, int z){
    return x+y+z;
  }

  // Method 3
  public int add(double x, int y){
    return (int)x+y;
  }
}

class Test{

  public static void main(String[] args){

    StaticPolymorphism demo=new StaticPolymorphism();

    // Method 1 called
    System.out.println(demo.add(2,3));

    // Method 2 called
    System.out.println(demo.add(2,3,4));
    
    // Method 3 called
    System.out.println(demo.add(2.5,3));

  }
}

 

Runtime Polymorphism

If a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed. We can override methods only, not the variables (data members), so runtime polymorphism (Dynamic Method Dispatch) cannot be achieved by data members. Runtime polymorphism is essentially referred as method overriding.  In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable. For example :

// A Java program to illustrate Dynamic Method
class A{
  int x = 10;
  
  void m1(){
    System.out.println("Inside A's m1 method");
  }
}

class B extends A{
  int x = 20;
  
  // Overriding m1()
  void m1(){
    System.out.println("Inside B's m1 method");
  }
}

// Driver class
class Dispatch{
  public static void main(String args[]){
  
    // Object of type A
    A a = new A();
    
    // Object of type B
    B b = new B();
    
    // Obtain a reference of type A
    A ref;
    
    // Ref refers to an A object
    ref = a;
    
    // Calling A's version of m1()
    ref.m1();
    
    // Now ref refers to a B object
    ref = b;
    
    // Calling B's version of m1()
    ref.m1();
    
    // Data member of class A will be accessed
    System.out.println(ref.x);
  }
}

 

Polymorphism makes code flexible. If you need new functionality, you could write a new subclass. But since your code uses the superclass, your new class will work without any changes to the rest of your code.

A b = new B();

// Then you do all sorts of things with it:
List<A> formation = ...

// superclass is important especially if working with collections
formation.add(b);

// ...
b.m1();

// When you suddenly decide to change A to C.
A b = new C();

 

All above code will just work because they relies on the interface (read:public methods) provided by the general A class, and not from the actual implementation you provide at the beginning. In this way, you can pass on different implementations without altering your other code.

// If you hadn't used an A superclass and just written B and C in above example, then you have to replace
B b = new B();
// with
C c = new C();

 

 

Related Posts :-