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
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 ABSTRACTFUNCTION area() RETURNS REAL // no body! ABSTRACTFUNCTION 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.
FORi ← 1TO3 OUTPUTshapes[i].area() // calls correct area() for each shape type NEXTi
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!
Term
Definition
🎯
Mini Test — 4.1.3 OOP Inheritance
10 questions · 10 marks · 10 minutes
⏱ 10:00
Section A — Multiple Choice [5 marks]
Q1Which keyword in Cambridge 9618 pseudocode is used to indicate that a class inherits from a superclass?
Q2A Dog subclass inherits from Animal. In the Dog constructor, how should the Animal constructor be called?
Q3Method overriding requires that the overriding method in the subclass has:
Q4Which of the following correctly describes an IS-A relationship?
Q5An abstract class with an abstract method area() is inherited by Circle and Rectangle. What must Circle and Rectangle each do?
Section B — Short Answer [5 marks]
Q6Explain why polymorphism is a useful feature in OOP. Use an example to support your answer.
Mark schemePolymorphism allows code to work with objects of different types through a common interface — the same method call produces different results depending on the actual object type [1]; this means you can write generic code that doesn't need to know the specific type at compile time — e.g. an array of Shape objects can have area() called on each element, and the correct implementation (Circle, Rectangle, or Triangle) runs automatically at runtime [1]; benefit: code becomes more flexible and extensible — adding a new shape type requires only creating a new subclass with an area() override; no changes needed to existing code that calls area() [1].
Q7Write pseudocode for a class Vehicle with a PROTECTED attribute speed and a method accelerate(amount). Then write a subclass Car that inherits from Vehicle and adds a private attribute doors.
Mark schemeCLASS Vehicle — correct class declaration [1]; PROTECTED speed : REAL and PUBLIC PROCEDURE accelerate(amount:REAL) with speed ← speed + amount [1]; CLASS Car INHERITS Vehicle — correct inheritance syntax [1]; PRIVATE doors : INTEGER declared; PUBLIC PROCEDURE NEW(d:INTEGER) calling CALL SUPER.NEW() and doors ← d [1].
Q8Why might a programmer use an abstract class rather than a regular superclass?
Mark schemeAn abstract class enforces that all subclasses implement specific methods — it creates a contract/interface that subclasses must fulfil [1]; this ensures consistency: if Shape is abstract with abstract methods area() and perimeter(), you are guaranteed that every Shape subclass will have these methods available [1]; it prevents instantiation of a class that would be incomplete or meaningless on its own (e.g. a generic "Shape" with no dimensions makes no sense as an object) [1].
Q9Explain the difference between method overriding and method overloading.
Mark schemeMethod overriding: a subclass provides a new implementation of a method that exists in the superclass, with the SAME name and SAME parameters — this is across different classes (parent and child); used for polymorphism [1]; method overloading: two or more methods in the SAME class have the SAME name but DIFFERENT parameters (different number or types of parameters) — the correct one is chosen based on the arguments provided at the call site [1].
Q10A subclass Dog inherits from Animal. Animal has a PRIVATE attribute name. State how Dog can access the name attribute in a getter method it calls from Animal.
Mark schemeDog cannot directly access the PRIVATE name attribute — private means only Animal's own methods can access it [1]; Dog can call the getName() getter method defined in Animal — since getName() is PUBLIC and is inherited by Dog, Dog's objects can call myDog.getName() which internally accesses the private name attribute [1]. Alternatively: if name were changed to PROTECTED, Dog could access it directly — but the preferred OOP approach is to use the inherited getter.