Programming Companion
Java

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.

IB DP CS standard B3.2.1: Explain and apply inheritance to promote code reusability through parent–child hierarchies, extending existing classes and understanding access to parent members.

Start with the relationship, not the keyword

check_circle

Dog is an Animal

A Dog can reuse shared Animal state and behaviour, then add specialised details.

check_circle

Human is an Animal

A Human is another specialised Animal type with additional state and behaviour.

cancel

Habitat is not an Animal

An Animal may have a Habitat, but that whole–part relationship is not inheritance.

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

1new Dog(...)

The Dog constructor begins.

2super(name, energy)

The Animal constructor stores shared state.

3Dog-only field

The child stores specialised state.

4Completed object

The caller can use inherited and Dog-only behaviour.

1Dog(...)

Python creates the object and calls Dog.__init__.

2super().__init__(...)

The Animal initialiser stores shared state.

3Dog-only attribute

The child stores specialised state.

4Completed object

The caller can use inherited and Dog-only behaviour.

Inheritance does not erase access rules

Parent memberEffect in Java subclass code
publicInherited and accessible through normal visibility rules.
protectedAccessible to subclasses and through package rules.
privateSubclass code cannot name the member directly.
defaultAccessible inside the same package, not as a general cross-package permission.
Python namingEffect in subclass code
namePublic by convention and inherited normally.
_nameSignals protected/internal use by convention; Python does not enforce it.
__nameName-mangled to reduce accidental access and clashes; use parent operations rather than guessing the mangled name.
No default/package modifierPython does not reproduce Java package-private access.
Syllabus comparison: you still need to understand Java-style public, private, protected and default access because the standard names them. Python expresses similar design intent mainly through conventions and name mangling rather than enforced modifiers.

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.
Inheritance creates a relationship, not an excuse to share any convenient code. Prefer a has-a relationship when one object merely contains or uses another.
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

Challenge ID: PC-T22-C01 · Standards: B3.2.1

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

Scaffold available
An Animal parent class branching to Dog and Human child classes.

Inheritance Design and Diagnosis

Challenge ID: PC-T22-C02 · Standards: B3.2.1

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

Scaffold available
Dog shown as a valid Animal child while Habitat is shown as a separate has-a relationship.
Next boundary: this page reuses parent behaviour but does not yet teach runtime selection of overridden methods through a parent reference. That belongs to polymorphism.