OOPS Interview Questions · Question 20

What is dynamic binding, and how does it enable runtime polymorphism?

Interview preparation resource from Gate Smashers.

Interview-ready answer

Dynamic binding, or late binding, is the runtime selection of the method implementation to execute based on an object’s actual type rather than the reference variable’s declared type. It enables runtime polymorphism because a base-class or interface reference can refer to different concrete objects, and an overridden method call invokes the appropriate implementation for the object currently referenced.

Java example demonstrating dynamic bindingjava
class Animal {
    void speak() {
        System.out.println("Animal speaks");
    }
}

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

class Cat extends Animal {
    @Override
    void speak() {
        System.out.println("Meow");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a1 = new Dog();
        Animal a2 = new Cat();

        a1.speak(); // Prints "Woof"
        a2.speak(); // Prints "Meow"

        Animal[] zoo = { new Dog(), new Animal(), new Cat() };
        for (Animal a : zoo) {
            a.speak();
        }
    }
}
Understand it clearly

Definition

In object-oriented programming, dynamic binding determines the target of an overridable method call while the program is running. The compiler verifies that the method can be called through the declared reference type, while runtime dispatch selects the implementation associated with the actual object type.

Dynamic binding contrasts with static binding, in which the method target is determined at compile time.

How it enables runtime polymorphism

A reference declared as a base class or interface can hold objects of multiple implementing or derived types. When code calls an overridden method through that reference, the same call site can produce different behavior for different objects.

This lets client code depend on a common abstraction instead of concrete classes. New implementations can be used by the same client code as long as they conform to the required base class or interface.

  • Declared type: Determines which members may be called through the reference.
  • Actual object type: Determines which overridden implementation runs for a dynamically dispatched call.

Runtime dispatch

Languages use runtime dispatch mechanisms to locate the correct overridden method. The exact mechanism is language- and implementation-specific; for example, many compiled object-oriented language implementations use method tables, while dynamic languages may use runtime method lookup.

In Java, ordinary instance-method calls are generally dynamically bound, except for methods such as static, final, and private methods, which are not overridden in the same way.

Trade-offs

Dynamic binding improves extensibility and loose coupling because code can operate on abstractions while object-specific behavior remains in concrete classes. It can introduce dispatch overhead, although runtime systems may optimize many calls when the target can be determined safely.