What is Dynamic Binding in Java?

Dynamic Binding in Java, also known as late binding, happens when the actual method to be called is decided while the program is running. The Java Virtual Machine (JVM) handles this at runtime, choosing the right method based on the situation.

Dynamic binding, also known as run-time polymorphism, means that the behavior of a method call is determined while the program is running, not during compilation. In simple terms, it’s not possible to know at compile-time which code will execute for a given method call.

A common example of dynamic binding in Java is method overriding, where a subclass provides its own version of a method defined in the parent class.


How Dynamic Binding Works in Java?

Dynamic binding occurs when the method to be executed is determined at runtime based on the actual object being referenced, rather than the reference type.

Let’s consider two classes, Animal and Dog, where Animal is the superclass and Dog is its subclass. If Dog overrides a method from class Animal, the following happens:

  • If a method is called on an object of type Dog, the implementation in class Dog is executed, regardless of whether the method is accessed through a reference of type Animal or Dog.
  • The Java Virtual Machine (JVM) dynamically binds the method call to the appropriate method implementation based on the actual object at runtime, not the reference type.

 

Example:

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

public class DynamicBindingDemo {
    public static void main(String[] args) {
        Animal animal = new Dog(); // Reference of type Animal, object of type Dog
        animal.makeSound();        // Calls the makeSound method in class Dog
    }
}
 

Output

Dog barks

In this example, even though the reference animal is of type Animal, the makeSound method in class Dog is executed because the actual object is of type Dog. This demonstrates dynamic binding in Java.

Let’s discuss conditions of Dynamic Binding or Runtime Polymorphism.

Also Read:

Structure of Java Program

Magic Number Program in Java

Conditions for Implementing Dynamic Binding

  1. Inheritance:
    In this condition, there should be one superclass, and one or more superclass is required, superclass inherit from superclass. The superclass should have a defined method which will be overwrite in subclass.
  2. Method Overriding:
    The subclass must override a method from the superclass. Method name and parameter types must match exactly in the superclass and subclass.
  3. Method Invocation:
    A reference variable of the superclass should be used to call the method on the subclass’s object. At runtime, the JVM determines which method implementation to execute based on the actual object type.
  4. Upcasting:
    The subclass object should be upcast to the superclass type. This ensures that the reference variable used to call the method matches the type of the superclass.

 

Limitations of Dynamic Binding

  1. Private Methods:
    Private methods in the parent class are not inherited by subclasses, so they cannot be overridden.
  2. Final Methods:
    Methods declared as final in the parent class cannot be overridden by any subclass.
  3. Static Methods:
    Static methods are associated with the class rather than the instance, making them ineligible for overriding. Instead, they follow the concept of method hiding.

Difference Between Static and Dynamic Binding

Static BindingDynamic Binding
Also known as early binding because it happens at compile-time.Also known as late binding because it happens at runtime.
Occurs during method overloading.Occurs during method overriding.
Does not involve real objects; the compiler determines the method to call based on the reference type.Involves real objects; the JVM decides which method to call based on the actual object type at runtime.
Typically applies to private, final, and static methods, as they cannot be overridden.Works with instance methods that are eligible for overriding.

Leave a Comment

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

Scroll to Top