Java OOP · Module 7
Animal a = new Dog(); a.sound();
Why the Dog version runs — the mechanism, step by step
What you'll be able to do
- Separate the reference type from the object's actual type
- Explain what the compiler decides and what the JVM decides
- Predict which implementation runs for any parent-typed reference
- Know why fields do not behave polymorphically
Dynamic Method Dispatch
Two types, two different questions
One line of code, two types: `Animal a = new Dog();`. Everything in this lesson follows from noticing that.
The famous two lines
Animal is the reference type (what the variable is declared as). Dog is the object type (what was actually created on the heap). The assignment is legal because a Dog IS-A Animal — this is *upcasting*, and it is automatic.Animal have a method sound()? Yes → the call is legal. At run time the JVM asks: what is this object really? A Dog → run Dog.sound(). That runtime step is dynamic method dispatch.Who decides what
| Question | Decided by | Based on |
|---|---|---|
| Is this call even allowed? | the compiler | the reference type (Animal) |
| Which method body runs? | the JVM, at run time | the object's real type (Dog) |
| Which overloaded version is picked? | the compiler | the argument types (Module 9) |
| Which field is read? | the compiler | the reference type — fields are never polymorphic |
Memorise the first two rows as one sentence: *the reference type decides what you may call, the object decides what actually happens.*
What is really in memory
The variable is just a labelled arrow typed `Animal`. The object on the heap never forgets that it is a Dog — that is the information the JVM uses to dispatch the call.
What happens when `a.sound()` executes
- The compiler looks up
sound()inAnimal. Found, no arguments, accessible → the code compiles. (Had you writtena.fetch(), compilation would fail here —Animalhas nofetch.) - At run time the JVM follows the reference to the object and reads its actual class:
Dog. - It looks for
sound()starting atDog. Found an override → that body runs. HadDognot overridden it, the search would continue up toAnimal. - Inside that body,
thisrefers to the Dog object — so any further unqualified calls dispatch dynamically too.
Run it: the Dog version wins. Now uncomment `a.fetch();` — the error proves the compiler only sees `Animal`.
So why is this useful, and not just clever?
Because it lets you write code against the parent type and have it work with subclasses that did not exist when you wrote it.
``java
static void checkout(Payment payment) {
payment.pay();
}
`
This method will correctly handle CardPayment, CashPayment, and the CryptoPayment your team adds next year — without a single edit, and without one if` checking the type. That is the practical value of overriding + dynamic dispatch, and it is what Module 8 formalises as polymorphism.
Prove it — coding challenges
Common mistakes
Animal a = new Dog(); a.fetch();Expecting `a.sound()` to print the Animal version because `a` is declared `Animal`.Assuming fields behave like methods.Lesson playground
An outbox of Notification references holding different subclasses. Add a `PushNotification` and put it in the array — the loop needs no change at all.
Check understanding
Quiz
Question 1 of 5
One method, three different objects. What is printed?
Summary
- `Animal a = new Dog();` has a reference type (`Animal`) and an object type (`Dog`).
- The compiler uses the reference type to decide what is callable; the JVM uses the object to decide which body runs.
- This lets one piece of code work with subclasses written later — no `if` on types.
- Fields (and static methods) are not polymorphic; instance methods are.