Java.Core.Explain dynamic method dispatch in Java

🌐 What is Dynamic Method Dispatch?

Dynamic Method Dispatch (also known as Runtime Polymorphism) is the mechanism where:

👉 The method to be called is determined at runtime, based on the actual object type — not the reference type.


📖 Explanation

  • In Java, you can have a reference of a parent class type that refers to an object of a child class.
  • When you call a method on that reference, the actual implementation in the child class is invoked — not the parent class version (if it’s overridden).
  • This is dynamic because the decision happens at runtime, not compile-time.

📚 Example

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

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

public class Test {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();  // Parent reference, child object
        myAnimal.makeSound();         // Which method gets called? The Dog version!
    }
}

output

Dog barks

✅ Even though myAnimal is declared as Animal, the Dog’s overridden method gets called at runtime.


⚙️ Key Rule — Method Lookup Happens at Runtime

  • At compile time, Java knows only that myAnimal is of type Animal.
  • At runtime, the JVM sees that the actual object is Dog, so it calls Dog‘s version of makeSound().

💡 Why is this useful?

✅ It allows Java to support polymorphism.
✅ You can write generic code that works for any subclass.

Example — Processing a list of animals, each behaving differently:

List<Animal> animals = List.of(new Dog(), new Cat(), new Bird());
for (Animal animal : animals) {
    animal.makeSound();  // Each animal makes its own sound!
}

🔥 Dynamic Dispatch Works Only for Overridden Methods

  • It works only when the method is overridden in the child class.
  • Fields are not dynamically dispatched — fields are resolved at compile time based on the reference type.

Example:

class Animal {
    String type = "Animal";
}

class Dog extends Animal {
    String type = "Dog";
}

public class Test {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        System.out.println(myAnimal.type);  // Prints "Animal", not "Dog"
    }
}

✅ Method = dynamic (runtime decision)
❌ Field = static (compile-time decision)


🏁 Quick Recap Table

Type of MemberHow Resolved?When Resolved?
Overridden MethodBased on actual object typeRuntime
FieldBased on reference typeCompile time

📊 Summary

FeatureDescription
PurposeEnables polymorphism
Works onOverridden methods only
Decision TimeRuntime
Based onActual object type (not reference type)

⚠️ Important Notes

✅ Works only for instance methods, not static methods (because static methods are resolved at compile time).
final, private, and static methods are not overridden, so they do not participate in dynamic dispatch.


📚 Final Tip for Interviews

✅ Always mention dynamic method dispatch = runtime polymorphism.
✅ Mention methods = runtime, fields = compile time.
✅ Highlight that it works through method overriding and allows you to write flexible, extensible code.

This entry was posted in Без рубрики. Bookmark the permalink.