Wednesday, June 16, 2021

Java OOPs: Polymorphism

Chapters

Java Polymorphism

Polymorphism simply means "many forms". In java, we can achieve polymorphism through method overloading and overriding. By using polymorphism, a method can have multiple forms with different functionalities.

There are two types of polymorphism in java: static(compile-time) polymorphism and dynamic(run-time) polymorphism.

Static Polymorphism And Static Binding

A method definition(form) and a call for that definition, which is determined at compile-time is called static polymorphism. In java, static polymorphism is achieved through method overloading. Moreover, a bind(connection) between overloaded method call and its definition is created during compile-time and that bind is called static binding.
Example

public class SampleClass{

  public static void main(String[]args){
  
    Increment inc = new Increment();
    
    System.out.println("Original Value: " + inc.getValue());
    //Calling increment() 1st form
    inc.increment();
    System.out.println("Incremented Value: " + inc.getValue());
    System.out.println();
    
    System.out.println("Original Value: " + inc.getValue());
    //Calling increment() 2nd form
    inc.increment(3);
    System.out.println("Incremented Value: " + inc.getValue());
    System.out.println();
    
     System.out.println("Original Value: " + inc.getValue());
    //Calling increment() 3rd form
    inc.increment(3,2);
    System.out.println("Incremented Value: " + inc.getValue());
    System.out.println();
  }
}

class Increment{
  private int value;
  
  //Form1
  public void increment(){
    value = ++value;
  }
  //Form2
  public void increment(int increase){
    value += increase;
  }
  //Form3
  public void increment(int increase,int incCount){
  
    for(int i = 0; i < incCount; i++)
      value += increase;
  }
  
  public int getValue(){ return value; }
}
When we compile the above example, java creates binds between increment() calls and their respective method definitions(forms). Thus, Overloaded forms are resolved at compile-time.

When we run the compiled version of the example above, Java won't need to create binds between increment() calls and their respective method definitions(forms) at run-time because it's already created during compilation process.

Dynamic Polymorphism And Dynamic Binding

A method definition(form) and a call for that definition, which is determined at run-time is called dynamic polymorphism. This process is also known as Dynamic Method Dispatch.

In java, dynamic polymorphism is achieved through method overriding. Moreover, a bind(connection) between overriding method call and its definition is created during run-time and that bind is called dynamic binding.
Example

public class SampleClass{

  public static void main(String[]args){
  
    Messenger1 m1 = new Messenger1();
    //Call Form1
    m1.displayMessage();
    
    Messenger2 m2 = new Messenger2();
    //Call Form2
    m2.displayMessage();
  }
}

abstract class Message{

  abstract void displayMessage();
}

class Messenger1 extends Message{
  
  //Form1
  @Override
  void displayMessage(){
    System.out.println("Message from Messenger1");
  }
}

class Messenger2 extends Message{

  //Form2
  @Override
  void displayMessage(){
    System.out.println("Message from Messenger2");
  }
}
If we compile the example above, the binds between overriding methods and the calls for them won't be created during compile time. Instead, it will be created once we run the compiled version of the example above.

No comments:

Post a Comment