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):
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.
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:
| Relationship | When to use | Example |
|---|---|---|
| Inheritance ('is-a') | When the subclass genuinely IS a more specific type of the superclass | A 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 it | A 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.