Skip to content
Ameen Abozaid
BEG-022

Inheritance

  • 7 Lessons
  • 74 min of reading
  • 3 h practice (estimate)
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

  1. Create a subclass with extends, call superclass constructors with super(...), and override methods with @Override.
  2. Explain what is and is not inherited.
  3. Apply the "is-a" test and choose composition when inheritance does not fit.

Lessons

  1. 01Reusing behavior with extends11 min read
  2. 02Constructors and super10 min read
  3. 03Overriding methods11 min read
  4. 04final classes and methods10 min read
  5. 05Composition over inheritance10 min read
  6. 06Demo: savings and checking accounts with extends11 min read
  7. 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)

IDOutcomeTask
BEG-022-EX011Create Animal (name, speak()) and Dog extends Animal that overrides speak() and calls super(name).
BEG-022-EX022List what a Dog object gets from Animal and what it does not (private fields, constructors).
BEG-022-EX033Write "is-a or has-a?" for: Car/Engine, Cat/Animal, Library/Book, Square/Rectangle.
BEG-022-EX043Rewrite 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/setHeight breaks 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 only log, 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.

  1. 1Which keyword creates a subclass?O1
  2. 2super(...) in a constructor must be:O1
  3. 3@Override helps because:O1
  4. 4How many direct superclasses can a Java class have?O1
  5. 5Which is NOT inherited by a subclass?O2
  6. 6A final method:O2
  7. 7Is-a or has-a: a Car and its Engine.O3
  8. 8Is-a or has-a: a SavingsAccount and an Account.O3
  9. 9Composition means:O3
  10. 10A good reason to avoid inheritance is:O3

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).

Type to search levels, phases, modules and lessons.