Sub topic 5
How Java Picks the Method at Runtime
You have seen that a variable declared as Animal can hold a Dog or a Cat. You have also seen that a subclass can override a method. This chapter puts those two ideas together. When you call a method through an Animal variable, which version runs?
The Short Answer
Java looks at the object, not at the variable.
This is called dynamic dispatch. Dynamic dispatch is the way Java chooses which method body to run while the program is running, based on the object's actual class. "Dispatch" means sending the call to the right place. "Dynamic" means the choice happens at runtime, not when the code compiles.
Two terms help here:
- The declared type is the type written next to the variable, such as
Animal a. - The runtime type is the class of the object the variable points to, such as
Dog.
The declared type decides which methods you are allowed to call. The runtime type decides which version actually runs.
A Worked Example
class Animal {
String sound() {
return "...";
}
}
class Dog extends Animal {
@Override
String sound() {
return "Woof";
}
}
class Cat extends Animal {
@Override
String sound() {
return "Meow";
}
}
public class Main {
public static void main(String[] args) {
Animal[] animals = { new Dog(), new Cat(), new Animal() };
for (Animal a : animals) {
System.out.println(a.sound());
}
}
}Save this as Main.java, then run javac Main.java and java Main. The output is:
Woof
Meow
...The loop line a.sound() never changes. The variable a is always declared as Animal. But each time around the loop, a points to a different kind of object. So a different sound() runs each time.
What Happens Step by Step
When Java runs a.sound(), it does this:
- Find the object that
apoints to. - Check that object's class for a
sound()method. - If the class has one, run it.
- If not, move up to the superclass and check again.
The JVM (Java Virtual Machine) is the program that runs your compiled Java code. It does this search for you on every call.
Two Checks, Two Times
Java checks a method call twice.
| When | What Java looks at | What it decides |
|---|---|---|
| Compile time | Declared type (`Animal`) | Whether the call is allowed |
| Runtime | Runtime type (`Dog`) | Which method body runs |
So if Dog had a method fetch() that Animal lacks, a.fetch() would not compile. The compiler only knows that a is some Animal.
Why This Matters
Dynamic dispatch lets you write one piece of code that works for many classes. The loop above does not need an if for each animal. If you add a Cow class later, the loop works with it unchanged.
Exercise
Add a class Puppy that extends Dog but does not override sound(). What does this print: Animal a = new Puppy(); System.out.println(a.sound());
Show answer
It prints "Woof". The runtime type is Puppy. Puppy has no sound() of its own, so Java moves up to its superclass Dog, finds Dog's sound(), and runs it.
Exercise
Given Animal a = new Cat(); and a Cat class that adds a method purr(), does a.purr() compile? Why or why not?
Show answer
No. The compiler checks the declared type, Animal, and Animal has no purr() method. The runtime type is used only to choose which version of an allowed method runs.