Java OOP · Module 6
extends: a child class
Animal → Dog / Cat, and the IS-A test that keeps you honest
What you'll be able to do
- Use `extends` to put shared state and behaviour in a parent class
- Say exactly which members a subclass inherits and which it does not
- Apply the IS-A test to decide whether inheritance is the right tool
- Distinguish `Dog extends Animal` from `Dog has an Animal`
Inheritance
One parent, many children
Three classes need `name`, `eat()` and `sleep()`. Copy them three times — or write them once and say 'Dog IS-A Animal'.
Suppose you write Dog, Cat and Bird. All three have a name, all three eat and sleep. Copy-pasting those members into each class means three places to fix every bug and three places to add every feature.
Inheritance lets one class take everything another class has and then add its own extras. The keyword is extends:
- The class you inherit from is the parent (also: superclass, base class).
- The class that inherits is the child (also: subclass, derived class).
The relationship it expresses is IS-A: a Dog *is a* Animal. That sentence is not decoration — it is the test that tells you whether inheritance is appropriate at all.
The base blueprint and the special editions
A car factory has one base blueprint: chassis, engine mount, four wheels, steering. The 'Taxi' edition takes the entire base blueprint and adds a meter and a roof sign. The 'Police' edition takes the same base and adds a siren. Nobody redraws the chassis. Change the base blueprint and every edition inherits the change — that is extends.
Animal → Dog / Cat
protected so subclasses may use it directly (Module 5). Had it been private, Dog would need getName() instead.Dog now has name, setName and eat without repeating them, and can add more.name as if it were declared here — because, from Dog's point of view, it is.Add a `Bird extends Animal` with a `fly()` method, then make a Bird eat and fly. Notice you write no `eat()` at all.
Java has single inheritance — and one hidden ancestor
A class may extend exactly one class: class A extends B, C does not compile. (Multiple *interfaces* are allowed — Module 11.) And every class you write already extends Object implicitly, which is where toString() and equals() come from.
Prove it — coding challenges
Inherited Fields
What is inherited — and what never is
Inheritance checklist
| Member of the parent | Inherited? | Notes |
|---|---|---|
| public / protected fields | ✅ | usable directly inside the subclass |
| package-private fields | ✅ same package only | invisible to a subclass in another package |
| private fields | ⚠️ exist, not accessible | the object still contains them; the subclass must use public/protected methods |
| public / protected methods | ✅ | can be used as-is or overridden (Module 7) |
| private methods | ❌ | not visible, cannot be overridden |
| constructors | ❌ never | but the child *calls* one with super(...) — next lesson |
| static members | ✅ accessible | they belong to the class, not the object |
IS-A vs HAS-A
IS-A vs HAS-A: `Dog extends Animal` or `Dog has an Animal`?
Both compile. Only one describes reality — and choosing wrong is the most expensive mistake in OOP design.
The same two classes, two relationships
IS-A — inheritance
Dog extends Animal
- Dog *is a kind of* Animal
- Dog can be used anywhere an Animal is expected
- Dog gets Animal's members automatically
- Right when the child is a specialisation
HAS-A — composition
Dog has an Animal (as a field)
- Dog *contains* an Animal — nonsense for animals
- No inherited members, no substitutability
- Perfect for
Car has an Engine(Module 12) - Right when the child owns a part
The two-question test before you type `extends`
- Say the sentence out loud: 'A Car IS-A Vehicle' ✅. 'A Car IS-A Engine' ❌ → that one is HAS-A.
- Then the substitution test: can the child be handed to *every* method that accepts the parent, without surprising it? If a
Penguin extends Birdwould breakbird.fly(), your hierarchy is wrong. - If both answers are not clearly yes, use composition — a field — instead.
Inheritance is for *what a thing is*, not for code reuse alone
Extending a class only because it happens to contain a method you want is the classic beginner trap (class InvoicePrinter extends ArrayList). You inherit its entire public API — and its future changes. Module 12 turns this into a design rule: favour composition over inheritance.
Common mistakes
class Car extends Engine { }Reading a parent's private field from the subclass.class A extends B, C { }Lesson playground
A Vehicle parent with two children. Add a `Truck extends Vehicle` with 6 wheels and a `load()` method — you should not need to rewrite `start()` or `info()`.
Check understanding
Quiz
Question 1 of 4
Circle declares no label and no describe(). What is printed?
Summary
- `class Child extends Parent` gives the child every accessible member of the parent, written once.
- Private members exist in the subclass object but are only reachable through inherited methods; constructors are never inherited.
- Java allows a single superclass, and every class silently extends `Object`.
- Use inheritance only when the IS-A sentence and the substitution test both hold — otherwise use a field (HAS-A).