Dynamic binding (also called late binding or runtime polymorphism) is a mechanism in Java where the method call is resolved at runtime based on the actual object type rather than the reference type. This is a core feature of method overriding in Java.
Key Concepts of Dynamic Binding
-
Works with Method Overriding:
- Only instance methods (non-static) can be dynamically bound.
- Static methods, variables, and private methods use static binding (resolved at compile time).
-
Determined at Runtime:
- The JVM decides which method to call based on the actual object type, not the reference type.
-
Enables Polymorphism:
- Allows a subclass to provide a specific implementation of a method defined in its superclass.
How Dynamic Binding Works
Example 1: Basic Dynamic Binding
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog(); // Upcasting (Parent reference, Child object)
myAnimal.sound(); // Output: "Dog barks" (Dynamic Binding)
}
}
Explanation:
-
myAnimal
is of typeAnimal
(reference type). - But the actual object is
Dog
. - At runtime, JVM checks the actual object type (
Dog
) and callsDog.sound()
instead ofAnimal.sound()
.
Example 2: Dynamic Binding with Multiple Classes
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle");
}
}
class Square extends Shape {
@Override
void draw() {
System.out.println("Drawing a square");
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Square();
shape1.draw(); // Output: "Drawing a circle"
shape2.draw(); // Output: "Drawing a square"
}
}
Explanation:
- The same
Shape
reference is used, but different objects (Circle
,Square
) execute their owndraw()
method.
Dynamic Binding vs Static Binding
Feature | Dynamic Binding (Late Binding) | Static Binding (Early Binding) |
---|---|---|
When resolved? | At runtime | At compile time |
Applies to | Overridden methods | Static methods, private methods, variables |
Example |
animal.sound() (actual object decides) |
Math.sqrt() (fixed at compile time) |
Performance | Slightly slower (runtime check) | Faster (compile-time decision) |
Why Use Dynamic Binding?
- Supports Polymorphism: Allows different objects to respond differently to the same method call.
- Flexibility: A superclass reference can point to any subclass object.
- Extensibility: New subclasses can be added without modifying existing code.
Real-World Use Case
class Bank {
float getInterestRate() {
return 0.0f;
}
}
class SBI extends Bank {
@Override
float getInterestRate() {
return 7.5f;
}
}
class HDFC extends Bank {
@Override
float getInterestRate() {
return 8.0f;
}
}
public class Main {
public static void main(String[] args) {
Bank bank1 = new SBI();
Bank bank2 = new HDFC();
System.out.println("SBI Rate: " + bank1.getInterestRate()); // 7.5
System.out.println("HDFC Rate: " + bank2.getInterestRate()); // 8.0
}
}
Explanation:
- Different banks provide different interest rates.
- The correct method is called at runtime based on the actual bank object.
Summary
- Dynamic binding happens at runtime.
- It applies to method overriding (not overloading).
- The actual object type determines which method is called.
- Enables polymorphism, making Java flexible and extensible.
This is why Java can implement runtime polymorphism efficiently! 🚀