Pro Content

Upgrade to access all Cambridge 9618 lessons including inheritance, method overriding, abstract classes, and polymorphism.

Upgrade to Pro →
← Back to Dashboard
📗 Paper 4 · 4.1 Further Programming
4.1.3 OOP — Inheritance, Abstract Classes & Polymorphism
Cambridge 9618 · International A Level Computer Science · ~18 min read
Notes
Video
Slides
Quiz
Worksheet

Inheritance

Inheritance allows a class (the subclass, child class, or derived class) to inherit all the attributes and methods from another class (the superclass, parent class, or base class). The subclass can then add new attributes/methods, or override inherited methods.

This models the IS-A relationship: a Dog IS-A Animal; a SavingsAccount IS-A BankAccount; a Circle IS-A Shape.

Inheritance hierarchy — Shape example

Shape (superclass)
colour : String
area() : Real
perimeter() : Real
↓ inherits
Circle (subclass)
+ inherited from Shape
radius : Real
area() — OVERRIDDEN
perimeter() — OVERRIDDEN
Rectangle (subclass)
+ inherited from Shape
width, height : Real
area() — OVERRIDDEN
perimeter() — OVERRIDDEN
Triangle (subclass)
+ inherited from Shape
base, height : Real
area() — OVERRIDDEN
perimeter() — OVERRIDDEN

Inheritance in Cambridge pseudocode

// Superclass (parent class)
CLASS Shape
  PROTECTED colour : STRING

  PUBLIC PROCEDURE NEW(c : STRING)
    colourc
  ENDPROCEDURE

  PUBLIC FUNCTION getColour() RETURNS STRING
    RETURN colour
  ENDFUNCTION
ENDCLASS

// Subclass (child class) — inherits from Shape
CLASS Circle INHERITS Shape
  PRIVATE radius : REAL

  PUBLIC PROCEDURE NEW(c : STRING, r : REAL)
    CALL SUPER.NEW(c)    // call parent constructor
    radiusr
  ENDPROCEDURE

  PUBLIC FUNCTION area() RETURNS REAL  // OVERRIDES Shape
    RETURN 3.14159 * radius * radius
  ENDFUNCTION
ENDCLASS

// Usage
myCircleNEW Circle("red", 5.0)
OUTPUT myCircle.area()     // 78.54
OUTPUT myCircle.getColour() // "red" — inherited from Shape

Method Overriding

Method overriding is when a subclass provides its own implementation of a method that already exists in the superclass, with the same name and parameters. The subclass version replaces the parent version for objects of the subclass type.

Shape.area() — parent version
FUNCTION area() RETURNS REAL
  RETURN 0  // default/placeholder
ENDFUNCTION
Circle.area() — overrides parent
FUNCTION area() RETURNS REAL
  RETURN 3.14159 * radius²
ENDFUNCTION

When myCircle.area() is called, the Circle's overridden version runs — not the parent's. This is runtime polymorphism (also called late binding or dynamic dispatch).

Abstract Classes and Abstract Methods

An abstract class is a class that cannot be instantiated directly — you cannot create objects of an abstract class. It exists purely to be a base class for subclasses to inherit from.

An abstract method is a method declared in an abstract class without any implementation — just the signature. Any concrete (non-abstract) subclass MUST provide an implementation.

ABSTRACT CLASS Shape
  PROTECTED colour : STRING
  ABSTRACT FUNCTION area() RETURNS REAL        // no body!
  ABSTRACT FUNCTION perimeter() RETURNS REAL  // no body!
Any class that inherits from Shape MUST override area() and perimeter(). The abstract class guarantees all shapes have these methods, but each shape implements them differently. You cannot write: myShape ← NEW Shape() — this would be an error.

Polymorphism

Polymorphism (Greek: "many forms") means the same method name works differently depending on the type of object it is called on. It is enabled by method overriding.

Circle
area()
→ π × r²
= 78.54
Rectangle
area()
→ w × h
= 50.00
Triangle
area()
→ ½bh
= 24.00

The power of polymorphism: you can write code that works with a Shape reference and calls area() — and the correct implementation runs automatically based on the actual object type. This allows code to work with objects of different types through a common interface.

// Polymorphism in action — array of Shape objects
DECLARE shapes[1:3] OF Shape
shapes[1] ← NEW Circle("red", 5.0)
shapes[2] ← NEW Rectangle("blue", 10.0, 5.0)
shapes[3] ← NEW Triangle("green", 8.0, 6.0)

FOR i1 TO 3
  OUTPUT shapes[i].area()  // calls correct area() for each shape type
NEXT i
Cambridge 9618 exam tip: Know the terminology — superclass/parent/base vs subclass/child/derived are all accepted synonyms. Be able to write a subclass using INHERITS keyword and CALL SUPER.NEW() in the constructor. Explain method overriding: same name and parameters in subclass replaces the parent's version. Explain abstraction from abstract classes: forces subclasses to implement specific methods — creates a guaranteed interface. Key polymorphism definition: the same method name operates differently based on the object type at runtime. IS-A test: if "A IS-A B" makes sense, then inheritance is appropriate (a Circle IS-A Shape ✓; a Car IS-A Engine ✗ — that's HAS-A, not IS-A).
⚠️ Common Mistakes
  • Confusing overriding and overloading — overriding: same name and same parameters, in a subclass (different class). Overloading: same name but DIFFERENT parameters, in the same class. Cambridge 9618 focuses on overriding.
  • Forgetting to call the parent constructor — if the subclass has a constructor (PROCEDURE NEW), it should call CALL SUPER.NEW(...) to initialise the inherited attributes from the parent. Without this, inherited attributes may not be initialised.
  • Trying to instantiate an abstract class — you cannot create objects of an abstract class. Abstract classes exist to be inherited from, not instantiated directly. myShape ← NEW Shape() is an error if Shape is abstract.
  • Saying inheritance is always "copying" parent code — inheritance is NOT copying. The subclass reuses the parent's methods without copying them. The actual method code exists once in the parent class and is used by all subclasses.
  • Confusing IS-A (inheritance) with HAS-A (composition/aggregation) — a Student IS-A Person (inheritance — Student extends Person). A Student HAS-A Course (composition — Student contains a Course object). HAS-A means the class has an attribute that is an object of another class.
  • Not knowing that PROTECTED allows subclass access — private attributes are inherited but NOT directly accessible by the subclass. Use PROTECTED for attributes the subclass needs to access directly.
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 4.1.3 OOP Inheritance & Polymorphism

8 questions · Cambridge 9618 standard

Q1Explain what is meant by inheritance in OOP. State one benefit of using inheritance.[3]
✅ Mark scheme
Inheritance is when a subclass (child class) acquires (inherits) the attributes and methods of a superclass (parent class) without those attributes and methods needing to be redefined [1]; the subclass can then add additional attributes and methods, and may override inherited methods with its own implementations [1]; benefit: code reuse — common attributes and methods are defined once in the parent class and automatically available in all subclasses, reducing duplication [1]. Accept: enables polymorphism, promotes a clear hierarchical class structure, easier maintenance (change parent, all children benefit).
Q2Describe what is meant by method overriding. Give an example using a Shape superclass with a Circle subclass.[3]
✅ Mark scheme
Method overriding is when a subclass provides its own implementation of a method that already exists in the superclass, using the same method name and same parameter types/number [1]; the subclass version replaces the parent version for objects of the subclass type — when the method is called on a subclass object, the subclass version runs [1]; example: Shape has area() returning 0 (default/abstract); Circle overrides area() with RETURN 3.14159 × radius² — calling area() on a Circle object uses Circle's version, not Shape's [1].
Q3What is an abstract class? Why can you not instantiate an abstract class?[3]
✅ Mark scheme
An abstract class is a class that contains one or more abstract methods — methods declared with a signature but no implementation body [1]; it is designed to be a base class only — subclasses must inherit from it and provide implementations for all abstract methods [1]; it cannot be instantiated because an abstract method has no body — if an object were created, calling the abstract method would fail as there is no code to execute; the class is incomplete by design and must be specialised through inheritance before an object can be created [1].
Q4Explain what polymorphism means in OOP and give an example demonstrating it.[3]
✅ Mark scheme
Polymorphism means the same method name behaves differently depending on the type of the object it is called on [1]; it is achieved through method overriding — subclasses override the same method name with type-specific implementations [1]; example: an array of Shape objects containing Circle, Rectangle, and Triangle instances — calling area() on each produces different results (π×r² for Circle; w×h for Rectangle) even though the same method name area() is used; the correct implementation is determined at runtime based on the actual object type [1].
Q5A class Dog inherits from a class Animal. Animal has a private attribute name. Can the Dog class directly access the name attribute? Explain and state what access modifier would allow it.[3]
✅ Mark scheme
No — private attributes are accessible only within the class they are declared in [1]; even though Dog inherits from Animal, it cannot directly access or modify name because it is declared as PRIVATE — only Animal's own methods can access it directly [1]; to allow Dog to access name directly, it should be declared as PROTECTED (#) — protected allows access from within the class and from any subclasses, but not from unrelated external code [1].
Q6Explain the difference between an IS-A relationship and a HAS-A relationship. Give one example of each.[4]
✅ Mark scheme
IS-A relationship (inheritance): a subclass IS a type of the superclass — the subclass shares the nature and behaviour of the parent class [1]; example: a Circle IS-A Shape; a Dog IS-A Animal; a SavingsAccount IS-A BankAccount — Circle/Dog/SavingsAccount should INHERIT from Shape/Animal/BankAccount [1]; HAS-A relationship (composition/aggregation): one class contains an object of another class as an attribute — one class "has" the other [1]; example: a Car HAS-A Engine (Engine is an attribute of Car); a Student HAS-A Course — Car/Student should contain an Engine/Course attribute, not inherit from them [1].
Q7Write Cambridge 9618 pseudocode for a Vehicle class with PRIVATE attributes make (STRING) and speed (REAL), and a PUBLIC method Accelerate(amount:REAL). Then write an ElectricVehicle class that INHERITS Vehicle, adds a PRIVATE batteryLevel (REAL) attribute, and overrides Accelerate so it also reduces batteryLevel by amount / 10.[6]
✅ Mark scheme
CLASS Vehicle [1]; PRIVATE make:STRING; PRIVATE speed:REAL [1]; PUBLIC PROCEDURE NEW(m:STRING) make←m; speed←0.0 ENDPROCEDURE; PUBLIC PROCEDURE Accelerate(amount:REAL) speed←speed+amount ENDPROCEDURE ENDCLASS [1]; CLASS ElectricVehicle INHERITS Vehicle [1]; PRIVATE batteryLevel:REAL; PUBLIC PROCEDURE NEW(m:STRING, bat:REAL) CALL SUPER.NEW(m); batteryLevel←bat ENDPROCEDURE [1]; PUBLIC PROCEDURE Accelerate(amount:REAL) CALL SUPER.Accelerate(amount); batteryLevel←batteryLevel-(amount/10) ENDPROCEDURE ENDCLASS [1].
Q8Explain the principle of "favour composition over inheritance". Give a concrete example where using inheritance would be INAPPROPRIATE, and show how composition would fix the problem. (Hint: think about a class hierarchy where a subclass cannot fulfil all of the superclass's method contracts.)[4]
✅ Mark scheme
Favour composition: rather than inheriting from a class to reuse behaviour, hold a reference to an object of that class as an attribute — "HAS-A" relationship instead of "IS-A" [1]; Inappropriate inheritance example: class Bird has Fly() method; class Penguin INHERITS Bird but cannot fly — overriding Fly() to do nothing (or raise an error) violates the Liskov Substitution Principle since a Penguin cannot be used as a Bird where Fly() is expected [1]; Composition fix: define a separate Flyable interface; give flying birds a Flyable attribute; Penguin simply does not have the Flyable attribute — no false inheritance [1]; Benefit: composition is more flexible — behaviours can be swapped at runtime; inheritance creates a rigid hierarchy [1].
Topic Quiz
Question 1 of 10
You scored
out of 10
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 4.1.3 OOP Inheritance

10 questions · 10 marks · 10 minutes

← 4.1.2 OOP Fundamentals
67 of 82 · Cambridge 9618
4.2.1 UML & Class Design →