Runtime Polymorphism in Java | Dynamic Binding in Java | Late Binding in Java

Run time polymorphism

Runtime Polymorphism in Java or Dynamic Binding in Java or Late Binding in Java


Runtime polymorphism is a concept in which object is not aware of binding during compile time. It will be decided at run time that which object is going to bind with which method. Method overriding is the part of dynamic binding or late binding in java.

This concept is widely used in project development and framework development in java. So anyhow, this concept needs to be clear.

In this tutorial, we will discuss about dynamic binding and how it happens at run time with some good explanation. But before we proceed let’s discuss each and every term one by one.

Run time polymorphism



What is dynamic Binding or late binding in java

In dynamic binding, an object is not aware of which method will be invoked at run time. That’s why called “late binding”, because binding happens at run time not at compile time. A reference variable may receive any type of object based on the requirement and then object will have to invoke it’s related methods.

Method overriding is the good example of dynamic binding in java. So let’s discuss about dynamic binding or late binding with an example.

class SmartPhone {

   public double getScreenSize() {
     return 4.0;
   }

   public int getNumberOfSimSupported() {
     return 1;
   }

   public String getBatteryStrength() {
     return "2000 mah";
   }

   public String getPhoneDetails() {
     return "Screen Size : " + this.getScreenSize() + ", No of Sims : "
            + this.getNumberOfSimSupported() + ", Battery : "
            + this.getBatteryStrength();
   }
}

class Samsung extends SmartPhone {

   public double getScreenSize() {
     return 5.5;
   }

   public int getNumberOfSimSupported() {
     return 2;
   }

   public String getBatteryStrength() {
     return "3500 mah";
   }

   public String getPhoneDetails() {
     return "Screen Size : " + this.getScreenSize() + ", No of Sims : "
            + this.getNumberOfSimSupported() + ", Battery : "
            + this.getBatteryStrength();
   }
}

class Micromax extends SmartPhone {

   public double getScreenSize() {
     return 5.0;
   }

   public int getNumberOfSimSupported() {
     return 2;
   }

   public String getBatteryStrength() {
     return "3900 mah";
   }

   public String getPhoneDetails() {
     return "Screen Size : " + this.getScreenSize() + ", No of Sims : "
            + this.getNumberOfSimSupported() + ", Battery : "
            + this.getBatteryStrength();
   }
}

public class DataTypeExample {

   public static void main(String[] args) {

     SmartPhone phone = null;
 
     Scanner input = new Scanner(System.in); 
     System.out.println("Choose your smart phone (1. Samsung 2. Micromax : ");
 
     int type = input.nextInt();

     switch (type) {
       case 1:
           phone = new Samsung();
           System.out.println(phone.getPhoneDetails());
           break;

       case 2:
           phone = new Micromax();
           System.out.println(phone.getPhoneDetails());
           break;

       default:
           phone = new SmartPhone();
           System.out.println(phone.getPhoneDetails());
           break;
    }

 }

}

Look at the above example, We have one parent class which is SmartPhone. SmartPhone class is now being extended by different vendors like Samsung, Micromax etc. Because each smart phone is different in size, color, battery life etc. So we have different methods which provides smart phone’s configuration details like number of sim supported, size and battery life. Then, we have one method which provides a quick details about the particular smart phone which is getPhoneDetails().

Now from the above example, it is clear that binding is not being performed during compile time. A reference variable will receive it’s object at run time as per user input.

When you will compile your java file, no object will be created. At run time, it will be decided which type of object needs to be created based on the user’s choice.

Note*: We are using Scanner class which can take user input at run time from the console. Scanner class take stream as an input, in our case which is “System.in”. So we can take input from the console and initialize our variables based on the type of the input like nextInt(), nextFloat(), nextLine() etc.

So, let’s discuss what’s happening here in above example.

  • Compiler will compile the java file and no object will be created at the time of compilation. A reference variable will be created with null value. Means, no object has been assigned to it yet.
  • Our program will start executing.
  • Scanner class will request user to input integer number as per instructions. Suppose, we enter 1 to get the details of Samsung smart phone.
  • The integer value will be assigned to the variable “type” with the value of “1” as per user input.
  • Next, switch statement will begin it’s execution and condition will be evaluated. And as per condition, case 1 will be executed.
  • Now reference variable “phone” of type SmartPhone class will be assigned an object of type Samsung. Reference variable phone is now pointing to an object of type Samsung.
  • Next, binding process will start. Both SmartPhone and Samsung have common method which is getPhoneDetails(). This method is overridden by class Samsung and Micromax.
  • In the case of dynamic binding, check will be performed on type of the object rather than type of reference.
  • So JVM will perform check on type of object which is Samsung, it will check do Samsung has any method named “getPhoneDetails()” which is true in this case. So JVM will bind overridden method getPhoneDetails().
  • Hence, you will get the details of Samsung mobile rather than SmartPhone.


In method overriding, child class method will always get first priority. If child class is not overriding method of base class then base class method will get priority.

Leave a Reply

Your email address will not be published. Required fields are marked *