SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
CAIE 9618 · Paper 4 · Topic 4.1.3

OOP Inheritance
& Polymorphism

Superclass · Subclass · INHERITS · Method Overriding · Polymorphism · Abstract Class

CSZone Cambridge International AS & A Level Computer Science 9618
Inheritance

Subclass Inherits from Superclass

// Superclass (base class)
CLASS Vehicle
PRIVATE Speed : REAL
PRIVATE Fuel : STRING

PUBLIC PROCEDURE NEW(s:REAL, f:STRING)
Speed ← s
Fuel ← f
ENDPROCEDURE

PUBLIC PROCEDURE Move()
OUTPUT "Vehicle moving at ", Speed
ENDPROCEDURE
ENDCLASS

// Subclass (derived class)
CLASS Car INHERITS Vehicle
PRIVATE NumDoors : INTEGER

PUBLIC PROCEDURE NEW(s:REAL, f:STRING, d:INTEGER)
CALL SUPER.NEW(s, f)
NumDoors ← d
ENDPROCEDURE
ENDCLASS
Vehicle (superclass)
Speed, Fuel, Move()
Car (subclass)
+ NumDoors
WHAT IS INHERITED?
All PUBLIC methods and attributes from the superclass
PRIVATE attributes of the superclass are inherited but not directly accessible — must use getters/setters
Subclass can add new attributes and methods
Subclass can override inherited methods
CAIE keyword: INHERITS. Call superclass constructor: CALL SUPER.NEW()
Polymorphism & Method Overriding

Same Name, Different Behaviour

Polymorphism means "many forms". A subclass can override a method from its superclass to provide its own implementation. The correct version is selected at runtime based on the actual object type.
// Superclass method
CLASS Animal
PUBLIC PROCEDURE MakeSound()
OUTPUT "..."
ENDPROCEDURE
ENDCLASS

// Dog overrides MakeSound
CLASS Dog INHERITS Animal
PUBLIC PROCEDURE MakeSound()
OUTPUT "Woof" // override
ENDPROCEDURE
ENDCLASS

// Cat overrides MakeSound
CLASS Cat INHERITS Animal
PUBLIC PROCEDURE MakeSound()
OUTPUT "Meow" // override
ENDPROCEDURE
ENDCLASS
WHY IS THIS USEFUL?
Code that processes a list of Animal objects can call .MakeSound() on each — the correct version runs automatically based on object type
Adding a new animal type (e.g. Bird) requires no changes to existing processing code
Reduces code duplication and improves maintainability
OVERRIDE vs OVERLOAD
Override — subclass redefines a method with the SAME signature from the superclass
Overload — same method name, DIFFERENT parameter list in the SAME class (not in CAIE 9618 pseudocode)
Exam Practice

Cambridge-style questions

Question 1
A class Shape has a method CalculateArea() that returns 0. Two subclasses Rectangle and Circle each override CalculateArea() with their own implementations. Explain the concept being demonstrated and state one benefit. [3]
1
This demonstrates polymorphism — the same method name (CalculateArea) has different implementations in different subclasses.
1
Each subclass overrides the inherited CalculateArea() method to provide its own specific formula (e.g. length × width for Rectangle; π × r² for Circle).
1
Benefit: code that processes a collection of Shape objects can call CalculateArea() on each — the runtime selects the correct version automatically, so no if-else logic is needed. Adding new shape types requires no changes to existing code (open-closed principle).
Common Mistakes

Don't lose easy marks

1
Confusing inheritance and polymorphism — inheritance is about reusing code from a superclass; polymorphism is about the same method name producing different behaviours in different subclasses. They are related but distinct concepts.
2
Forgetting to call CALL SUPER.NEW() in the subclass constructor — in CAIE pseudocode the subclass constructor must explicitly call the superclass constructor to initialise the inherited attributes. Omitting this leaves inherited attributes uninitialised.
3
Saying "the subclass gets a copy of the superclass code" — inheritance is not copying. The subclass IS-A superclass (e.g. Car IS-A Vehicle). The subclass object contains the superclass attributes and can use superclass methods directly — it is a single unified object.
Topic Summary — 4.1.3

What You Need to Know

INHERITANCE
CLASS Sub INHERITS Super. Subclass inherits all PUBLIC methods/attributes. Can add new members and override methods. Call SUPER.NEW() in subclass constructor. IS-A relationship.
POLYMORPHISM
Subclass overrides a superclass method with same signature. Correct version selected at runtime based on object type. Enables extensible code — add new subclasses without changing existing processing code.
KEY TERMS
Superclass/base class = parent. Subclass/derived class = child. Override = redefine same method. INHERITS keyword. SUPER.NEW() to call parent constructor. IS-A vs HAS-A (composition).
CSZone

Next Video

4.2.1
UML & Class Design
Class Diagrams · Associations · Multiplicity
Head to CSZone.co.uk for the complete worksheet, quiz, and interactive tools