Java OOP · Module 8
One reference, many forms
Animal animal = new Dog(); — dissected in four questions
What you'll be able to do
- Define polymorphism in terms of references and objects, not slogans
- Answer the four questions about any `Parent p = new Child();` line
- Distinguish compile-time polymorphism from runtime polymorphism
- Replace a chain of type checks with a polymorphic call
Polymorphism
What polymorphism actually buys you
Polymorphism means 'many forms'. In practice it means: your `for` loop never has to know which subclass it is holding.
You already built polymorphism in Module 7 — this lesson names it and pushes it further.
Polymorphism = the ability of one reference type to refer to objects of many different classes, and for the *object* to decide which behaviour runs. Two ingredients make it possible:
1. Inheritance (or interfaces) — so a Dog can legally be stored in an Animal variable.
2. Overriding — so Dog has its own version of the method to run.
Without it, every new type forces you to edit existing code. With it, existing code keeps working and new types simply plug in.
One power button, many devices
A universal remote has one power() button. Point it at the TV and the TV turns on; point it at the air conditioner and the AC starts. The button never changed — the *device* decides what 'power' means. Your code is the button; the object is the device.
`Animal animal = new Dog();` — the four questions
| Question | Answer | Why it matters |
|---|---|---|
| What is the reference type? | Animal — the declared type of the variable | it defines the *vocabulary*: the set of members you are allowed to use |
| What is the real object type? | Dog — what new created on the heap | the object keeps this identity for its whole life |
| What can I access? | only what Animal declares (sound(), eat(), ...) — *not* Dog-only members | animal.fetch() is a compile error even though the object has fetch() |
| Which method runs? | the Dog override, chosen at run time | this is dynamic method dispatch — the object always wins |
Reference type = what you may say. Object type = what actually happens. Every polymorphism puzzle is solved by answering these four questions in order.
Add a `Cow extends Animal` overriding `sound()` with 'Moo', then add `new Cow("Bella")` to the array. The loop stays untouched — that is the entire point.
The same feature, written twice
Without polymorphism
a chain that grows forever
- Every new type edits existing, tested code
- Forget one branch and it silently does nothing
- Behaviour lives far away from the class it belongs to
With polymorphism
closed for modification, open for extension
- New types plug in without touching callers
- The compiler forces each subclass to supply behaviour (Module 10)
- Each class owns its own logic
Interview one-liner
“Polymorphism lets me program against an abstraction instead of a concrete class, so behaviour is selected by the object at run time and new subtypes require no changes to existing code.” Then give the Animal[] loop or the Payment example — a concrete example after the definition is what separates a strong answer from a memorised one.
Prove it — coding challenges
Compile-time vs Runtime
Compile-time vs runtime polymorphism
Java makes two kinds of 'many forms' — and only one of them is decided while your program is running.
The two kinds
| Compile-time (static) | Runtime (dynamic) | |
|---|---|---|
| Mechanism | method overloading (Module 9) | method overriding |
| Decided by | the compiler | the JVM |
| Based on | the declared types of the arguments | the actual class of the object |
| Also called | early / static binding | late / dynamic binding, dynamic dispatch |
| Needs inheritance? | no | yes (or an interface) |
| Example | add(2, 3) vs add(2.5, 3.5) | animal.sound() on a Dog |
When people say 'polymorphism' without qualification, they almost always mean the runtime kind — that is the one that changes how you design systems. Overloading is a convenience for callers; overriding is a lever for architecture. A useful way to keep them apart: overloading asks *'which method did you mean?'* and is answered before the program runs. Overriding asks *'whose version of this method?'* and can only be answered once a real object exists.
Common mistakes
Believing polymorphism means 'a method with many versions' (overloading).Using `instanceof` chains to pick behaviour for each subclass.Declaring `Dog dog = new Dog();` everywhere and passing `Dog` into every method.Lesson playground
`report(Shape)` is written once against the parent type. Add a `Triangle` (area = base * height / 2) and report it — without editing `report`.
Check understanding
Quiz
Question 1 of 5
Dog overrides one method and inherits the other. What does this print?
Summary
- Polymorphism = one reference type, many object types, behaviour chosen by the object.
- Reference type decides what you can call; the object decides what runs.
- Overloading is compile-time polymorphism; overriding is runtime polymorphism.
- A polymorphic call replaces an `instanceof` chain and survives new subclasses unchanged.