Programming technique · B3.2.1 · Higher level only
Inheritance
Inheritance allows a specialised child class to reuse and extend a defensible parent-class design. The code is usually short; the important decision is whether the relationship genuinely represents an is-a hierarchy.
Start with the relationship, not the keyword
Dog is an Animal
A Dog can reuse shared Animal state and behaviour, then add specialised details.
Human is an Animal
A Human is another specialised Animal type with additional state and behaviour.
Habitat is not an Animal
An Animal may have a Habitat, but that whole–part relationship is not inheritance.
Parent class
Animal
shared name, energy and behaviour
Child class
Dog
toy and play behaviour
Child class
Human
language and exercise behaviour
Apply the hierarchy in Java
extends establishes the relationship. Each child constructor uses super(...) to construct the Animal part before storing specialised state.
Apply the hierarchy in Python
Put the parent class in parentheses, such as class Dog(Animal). Each child initialiser calls super().__init__(...) before storing specialised state.
Trace construction from parent to child
new Dog(...)The Dog constructor begins.
super(name, energy)The Animal constructor stores shared state.
The child stores specialised state.
The caller can use inherited and Dog-only behaviour.
Dog(...)Python creates the object and calls Dog.__init__.
super().__init__(...)The Animal initialiser stores shared state.
The child stores specialised state.
The caller can use inherited and Dog-only behaviour.
Inheritance does not erase access rules
| Parent member | Effect in Java subclass code |
|---|---|
| public | Inherited and accessible through normal visibility rules. |
| protected | Accessible to subclasses and through package rules. |
| private | Subclass code cannot name the member directly. |
| default | Accessible inside the same package, not as a general cross-package permission. |
| Python naming | Effect in subclass code |
|---|---|
name | Public by convention and inherited normally. |
_name | Signals protected/internal use by convention; Python does not enforce it. |
__name | Name-mangled to reduce accidental access and clashes; use parent operations rather than guessing the mangled name. |
| No default/package modifier | Python does not reproduce Java package-private access. |
Why use inheritance—and when not to
Useful when
- the child genuinely satisfies an is-a relationship;
- classes share stable state or behaviour;
- specialised classes extend rather than duplicate the parent design.
Costs and risks
- children become coupled to parent decisions;
- a weak hierarchy spreads unsuitable behaviour;
- deep hierarchies are harder to trace and maintain.
Challenges Choose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
Complete the Animal Hierarchy
SelectedComplete the supplied Animal, Dog and Human hierarchy. Store shared name and energy state in Animal, call the parent constructor with super(...), keep specialised fields in the correct subclasses and use inherited public or protected operations rather than directly reaching private parent fields. Create one Dog and one Human, call inherited and specialised behaviour, and trace the final independent state of both objects.
Inheritance Design and Diagnosis
SelectedRepair the supplied Dog subclass by correcting constructor chaining and private-member access. Then add concise comments that justify Dog is-an Animal, reject Habitat is-an Animal, replace it with an Animal has-a Habitat relationship, and explain one reuse benefit and one coupling or maintenance cost of the hierarchy.
Selected challenge
This choice is shared with the portfolio setup page.
Create your challenge folder
Run this command before starting. It creates the correct empty folder inside your portfolio.
Complete the challenge
Write and test your own solution in the folder created above.
Optional scaffolded support
Use only as much support as you need. The templates organise the program but leave the important algorithm unfinished.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it.