Dynamic Method Dispatch
About
Example
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Test {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound(); // Outputs: Dog barks
a = new Cat();
a.sound(); // Outputs: Cat meows
}
}Rules for Dynamic Method Dispatch
How It Works Internally ?
1. Virtual Method Table (vtable) Mechanism
2. Object Instantiation and vtable Binding
3. Why Not Compile-Time ?
Example
Last updated