CodeIEB
Theory Notes/🗄️ Topic 4: Data & Information Management, Solution Development/12.4.5
12.4.5Grade 12

Inheritance & Advanced OOP

The capstone OOP subtopic — inheritance is one of the most heavily examined concepts in the entire Grade 12 syllabus. Master this thoroughly.

Extended objects (building on 11.4.3/11.4.5):

  • Fields of complex types — a field that is itself an object, or an array of objects.
  • Parameter passing and return of complex types — passing/returning whole objects or arrays of objects, not just primitives.
  • Null objects/fields — an object reference or field that has no value assigned, requiring careful handling to avoid runtime errors when accessed.
Superclass (parent class)
The general class that other, more specific classes inherit from.
Subclass (child class)
A class that inherits fields and methods from a superclass, and can add its own additional fields/methods or override inherited ones.

Example

public class Vehicle { protected String registration; public void startEngine() { System.out.println("Engine started"); } } public class Car extends Vehicle { private int numberOfDoors; // Car inherits registration and startEngine() automatically }

Car and Motorcycle both inherit from the Vehicle superclass — the arrow points from subclass to superclass.

Overriding — a subclass provides its own specific implementation of a method that already exists in its superclass, replacing the inherited behaviour for objects of that subclass.

Polymorphism
The ability for objects of different classes (related by inheritance) to be treated through a common superclass type, while each still responding to a shared method call in its own overridden way.
Dynamic binding
The mechanism that determines, at runtime, exactly which overridden version of a method should actually execute, based on the real (actual) type of the object — not the declared type of the variable.

Example

Vehicle myVehicle = new Car(); // declared type: Vehicle, actual type: Car myVehicle.startEngine(); // if Car overrides startEngine(), the Car version runs (dynamic binding)

Advantages of inheritance: avoids duplicating shared code across similar classes (code efficiency/reuse), creates a logical, organised class hierarchy that mirrors real-world relationships, and makes future maintenance easier since shared behaviour only needs to be updated in one place (the superclass).

Object as a field vs inheritance — an important design decision:

RelationshipWhen to useExample
Inheritance ('is-a')When the subclass genuinely IS a more specific type of the superclassA Car IS A Vehicle
Object as a field / composition ('has-a')When one class simply CONTAINS or USES another, without being a specialised type of itA Car HAS AN Engine (not 'is an' Engine)

Type determination — using an 'instance of' check (Java: instanceof) to determine an object's actual runtime type, useful when working with a collection of superclass-typed objects that are actually a mix of different subclasses.

Example

if (myVehicle instanceof Car) { Car myCar = (Car) myVehicle; // now safe to use Car-specific fields/methods }

You may also be asked to compare different data structures (arrays, arrays of objects, inheritance hierarchies) and justify the advantages of one approach over another for a given scenario.

💡 Exam Tip

The classic 'is-a vs has-a' exam trap: students often default to inheritance for every relationship. Ask yourself the sentence test — does 'X is a Y' make logical sense? If not (it should be 'X has a Y'), use composition/object-as-a-field instead.