Learn how Java resolves overridden methods at runtime using reference types and object instances.
Reference Type (Type on the left) determines what methods are accessible at compile time.
Actual Object Type (Type on the right) determines which implementation runs at runtime.
SuperClass obj = new SubClass();
class Animal {
void makeSound() {
System.out.println("Some generic sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Woof! Woof!");
}
}
class Cat extends Animal {
void makeSound() {
System.out.println("Meow! Meow!");
}
}
Select an object type to instantiate under the Animal reference, then dispatch makeSound().
myAnimalAnimal.makeSound()