Inheritance
On this page 8 topics
Overview
Topics
- extends
- superclass/subclass
- overriding
- super
- composition vs inheritance
Learn first
74 min of reading · 3 h of practice (estimate)
Learning outcomes
- Create a subclass with
extends, call superclass constructors withsuper(...), and override methods with@Override. - Explain what is and is not inherited.
- Apply the "is-a" test and choose composition when inheritance does not fit.
Lessons
- 01Reusing behavior with extends11 min read
- 02Constructors and super10 min read
- 03Overriding methods11 min read
- 04final classes and methods10 min read
- 05Composition over inheritance10 min read
- 06Demo: savings and checking accounts with extends11 min read
- 07Demo: refactoring bad inheritance into composition11 min read
Lab and exercises
Lab
BEG-022-LAB1 · Bank Accounts v3
Outcomes: 1, 2, 3 · Time: ~2 h Create SavingsAccount (interest) and CheckingAccount (overdraft) extending Account, using super(...) and @Override. Then refactor a provided bad inheritance hierarchy into composition and write a short explanation of why. Acceptance: overrides correct; refactor explained.
Exercises (assessment: 4 exercises plus a written "is-a or has-a?" quiz)
| ID | Outcome | Task |
|---|---|---|
| BEG-022-EX01 | 1 | Create Animal (name, speak()) and Dog extends Animal that overrides speak() and calls super(name). |
| BEG-022-EX02 | 2 | List what a Dog object gets from Animal and what it does not (private fields, constructors). |
| BEG-022-EX03 | 3 | Write "is-a or has-a?" for: Car/Engine, Cat/Animal, Library/Book, Square/Rectangle. |
| BEG-022-EX04 | 3 | Rewrite class Logger extends FileWriter as composition and explain the benefit. |
Show solutions
- EX01
java class Animal { protected final String name; Animal(String name) { this.name = name; } String speak() { return "..."; } } class Dog extends Animal { Dog(String n) { super(n); } @Override String speak() { return "Woof"; } } - EX02 Gets: non-private fields and methods (private ones exist inside the object but are not accessible). Does not get: constructors; private members are not accessible.
- EX03 has-a; is-a; has-a; Square/Rectangle: has-a is the safer answer (a square inheriting
setWidth/setHeightbreaks rectangle behavior, so "is-a" in maths does not mean "extends" in code). - EX04
class Logger { private final FileWriter out; ... void log(String m) { out.write(m); } }. Callers see onlylog, and the writer can be swapped. - The written "is-a or has-a?" quiz is the module quiz; the Account hierarchy and override outcomes are also covered by the lab.
Quiz
Outcomes: O1 extends/super/override · O2 what is inherited · O3 is-a vs has-a.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Four exercises BEG-022-EX01 to EX04, the written is-a/has-a quiz above, and Bank Accounts v3. Pass: quiz 7/10, at least three exercises correct, and the refactor explained.
Common mistakes
Inheritance for code reuse only; deep hierarchies; forgetting @Override.
Assessment
4 exercises plus a written "is-a or has-a?" quiz.
Career relevance
Framework extension points (JNR-008) and design patterns (JNR-005).