🧩 OOP Concepts Intermediate

What is the difference between early and late binding?

Why Interviewers Ask This

This tests whether you can apply OOP Concepts knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

Binding is the process of associating a method call with the method's definition (code). Timing of this association determines whether binding is early or late: Early binding (static binding, compile-time binding): the method to be called is determined at COMPILE time, based on the declared type of the reference. Occurs with: overloaded methods, static methods, private methods, final methods: class Animal { public static void staticMethod() { System.out.println("Animal static"); } public final void finalMethod() { System.out.println("Animal final"); } } class Dog extends Animal { public static void staticMethod() { System.out.println("Dog static"); } } // Static method -- early binding: Animal a = new Dog(); a.staticMethod(); // "Animal static" -- decided at compile time based on declared type!. Late binding (dynamic binding, runtime binding): the method to be called is determined at RUNTIME based on the actual type of the object (not the declared reference type). Occurs with virtual/overridden methods: class Animal { public void speak() { System.out.println("Some sound"); } } class Dog extends Animal { @Override public void speak() { System.out.println("Woof!"); } } Animal a = new Dog(); a.speak(); // "Woof!" -- determined at RUNTIME because Dog object is there!. How late binding works: Java uses vtable (virtual method table) — each class has a table of method pointers. When speak() is called on a reference, the runtime looks up the actual class's vtable and calls the right method. Performance: early binding is faster (direct call); late binding adds one indirection. In Java: all instance methods are late-bound by default (virtual). Use final/static/private for early binding (and performance when needed).

Common Mistake

A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.