Pro Content

Upgrade to access all Cambridge 9618 lessons including full OOP — classes, inheritance and polymorphism in pseudocode.

Upgrade to Pro →
← Back to Dashboard
🔢 Paper 2 · 2.5 Programming Paradigms
2.5.2 Object-Oriented Programming
Cambridge 9618 · International A Level Computer Science · ~20 min read
Notes
Video
Slides
Quiz
Worksheet

Class Definition in Cambridge 9618 Pseudocode

A class is a template that defines the attributes and methods for a type of object. In Cambridge 9618 pseudocode, classes are defined using CLASS...ENDCLASS.

CLASS Animal
  PRIVATE name : STRING
  PRIVATE age : INTEGER

  PUBLIC PROCEDURE NEW(n : STRING, a : INTEGER)
    name ← n  // constructor — sets initial attribute values
    age ← a
  ENDPROCEDURE

  PUBLIC PROCEDURE SetName(n : STRING)
    name ← n
  ENDPROCEDURE

  PUBLIC FUNCTION GetName() RETURNS STRING
    RETURN name
  ENDFUNCTION

  PUBLIC PROCEDURE Describe()
    OUTPUT name & " is " & age & " years old"
  ENDPROCEDURE
ENDCLASS

Class Diagram Representation

Animal
Attributes
- name : STRING
- age : INTEGER
Methods
+ NEW(n : STRING, a : INTEGER)
+ SetName(n : STRING)
+ GetName() : STRING
+ Describe()

Notation: - (minus) = PRIVATE   |   + (plus) = PUBLIC

Creating and Using Objects

// Declare an object variable
DECLARE myAnimal : Animal

// Create an instance using the constructor
myAnimal ← NEW Animal("Lion", 5)

// Call methods using dot notation
myAnimal.Describe()  // outputs: Lion is 5 years old
myAnimal.SetName("Tiger")
OUTPUT myAnimal.GetName()  // outputs: Tiger

The Four Pillars of OOP

🔒
Encapsulation
Bundling attributes and methods inside a class. Private attributes are hidden — only accessible through public methods (getters/setters). Protects data integrity.
🧬
Inheritance
A subclass inherits all attributes and methods from a superclass using INHERITS keyword. The subclass can add new attributes/methods and override inherited methods.
🎭
Polymorphism
Different objects respond to the same method call in different ways. A subclass can override a superclass method — the overridden version runs for subclass objects.
🗺️
Abstraction
Hiding complex implementation details and exposing only a clean interface. Users of a class interact with it through its public methods without knowing the internal code.

Inheritance

A subclass (also called child class or derived class) inherits all the public and protected attributes and methods of a superclass (parent class). Use the INHERITS keyword in Cambridge 9618 pseudocode.

CLASS Dog INHERITS Animal
  PRIVATE breed : STRING

  PUBLIC PROCEDURE NEW(n : STRING, a : INTEGER, b : STRING)
    CALL SUPER.NEW(n, a)  // call parent constructor
    breed ← b
  ENDPROCEDURE

  PUBLIC FUNCTION GetBreed() RETURNS STRING
    RETURN breed
  ENDFUNCTION

  PUBLIC PROCEDURE Describe()  // override superclass method
    OUTPUT GetName() & " is a " & breed
  ENDPROCEDURE
ENDCLASS

Using the Subclass

DECLARE myDog : Dog
myDog ← NEW Dog("Rex", 3, "Labrador")
myDog.Describe()  // uses OVERRIDDEN version → "Rex is a Labrador"
OUTPUT myDog.GetName()  // inherited from Animal → "Rex"
OUTPUT myDog.GetBreed()  // Dog's own method → "Labrador"

Encapsulation — Getters and Setters

Because attributes are PRIVATE, external code cannot access them directly. Getter methods return an attribute value; setter methods update it. This allows validation inside the setter.

// Direct access would fail — name is PRIVATE:
OUTPUT myAnimal.name  // ERROR — private attribute

// Correct approach — use getter:
OUTPUT myAnimal.GetName()  // OK — public method

// Setter with validation example:
PUBLIC PROCEDURE SetAge(a : INTEGER)
  IF a >= 0 THEN
    age ← a
  ELSE
    OUTPUT "Age cannot be negative"
  ENDIF
ENDPROCEDURE

Polymorphism

When a subclass overrides a superclass method, calling that method on a subclass object uses the overridden version. This lets the same method name behave differently for different object types.

DECLARE a1 : Animal
DECLARE d1 : Dog
a1 ← NEW Animal("Cat", 2)
d1 ← NEW Dog("Rex", 3, "Labrador")

a1.Describe()  // Animal.Describe() → "Cat is 2 years old"
d1.Describe()  // Dog.Describe() (overridden) → "Rex is a Labrador"
// Same method name, different behaviour — this is POLYMORPHISM

Full Example — Bank Account System

CLASS Account
  PRIVATE owner : STRING
  PRIVATE balance : REAL

  PUBLIC PROCEDURE NEW(o : STRING, b : REAL)
    owner ← o
    balance ← b
  ENDPROCEDURE

  PUBLIC PROCEDURE Deposit(amount : REAL)
    IF amount > 0 THEN
      balance ← balance + amount
    ENDIF
  ENDPROCEDURE

  PUBLIC FUNCTION GetBalance() RETURNS REAL
    RETURN balance
  ENDFUNCTION
ENDCLASS

CLASS SavingsAccount INHERITS Account
  PRIVATE interestRate : REAL

  PUBLIC PROCEDURE NEW(o : STRING, b : REAL, r : REAL)
    CALL SUPER.NEW(o, b)
    interestRate ← r
  ENDPROCEDURE

  PUBLIC PROCEDURE AddInterest()
    Deposit(GetBalance() * interestRate)
  ENDPROCEDURE
ENDCLASS
Cambridge 9618 pseudocode syntax rules for OOP: CLASS/ENDCLASS for class definition; PRIVATE/PUBLIC access modifiers; NEW as the constructor name; INHERITS for inheritance; CALL SUPER.NEW() to invoke the parent constructor; dot notation for method calls (obj.Method()); DECLARE obj : ClassName then obj ← NEW ClassName(args) to create objects.
Getter/Setter naming convention: Cambridge mark schemes expect getters named GetX() returning the attribute, and setters named SetX(value) updating the attribute. Always make attributes PRIVATE and provide public getter/setter methods.
⚠️ Common Mistakes
  • Accessing private attributes directly (e.g. obj.name) — always use getter methods
  • Forgetting to call SUPER.NEW() in a subclass constructor — parent attributes won't be initialised
  • Confusing 'class' and 'object' — class is the template (no data), object is the instance (has data)
  • Not using dot notation to call methods — write myDog.Describe(), not Describe(myDog)
  • Writing constructor as FUNCTION instead of PROCEDURE — NEW is a PROCEDURE (returns nothing)
  • Forgetting ENDCLASS — every CLASS definition needs ENDCLASS at the end
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.5.2 Object-Oriented Programming

8 questions · Cambridge 9618 standard

Q1Define a class called Rectangle with PRIVATE attributes width and height (both REAL). Include a PUBLIC constructor NEW that takes two REAL parameters, and a PUBLIC FUNCTION Area() that returns the area.[6]
✅ Mark scheme
CLASS Rectangle [1]; PRIVATE width : REAL; PRIVATE height : REAL [1]; PUBLIC PROCEDURE NEW(w : REAL, h : REAL) [1]; width ← w; height ← h; ENDPROCEDURE [1]; PUBLIC FUNCTION Area() RETURNS REAL [1]; RETURN width * height; ENDFUNCTION [1]; ENDCLASS.
Q2Write pseudocode to: (a) declare an object myRect of class Rectangle, (b) create a Rectangle with width=5.0 and height=3.0, (c) output the area.[3]
✅ Mark scheme
DECLARE myRect : Rectangle [1]; myRect ← NEW Rectangle(5.0, 3.0) [1]; OUTPUT myRect.Area() [1].
Q3Explain what 'encapsulation' means in OOP and state why attributes are usually declared as PRIVATE.[3]
✅ Mark scheme
Encapsulation bundles attributes and methods together inside a class and hides internal implementation details from external code [1]; attributes are PRIVATE so external code cannot access or modify them directly [1]; this protects data integrity — only class methods can change attribute values, allowing validation to be included in setter methods [1].
Q4Define a class Square that INHERITS from Rectangle. Square has only one dimension (side). Its constructor should take one REAL parameter and call the parent constructor appropriately.[5]
✅ Mark scheme
CLASS Square INHERITS Rectangle [1]; PUBLIC PROCEDURE NEW(s : REAL) [1]; CALL SUPER.NEW(s, s) [1]; ENDPROCEDURE [1]; ENDCLASS [1]. (Award marks for correct INHERITS, constructor with one parameter, calling SUPER.NEW with both dimensions as s, and correct ENDCLASS.)
Q5Explain polymorphism using an example from OOP. How does method overriding relate to polymorphism?[4]
✅ Mark scheme
Polymorphism means different objects can respond to the same method call in different ways [1]; a subclass can override a superclass method to provide a different implementation [1]; example: Animal.Describe() outputs "name is age years old"; Dog.Describe() (overridden) outputs "name is a breed" — both objects respond to Describe() but with different behaviour [1]; when Describe() is called on a Dog object, the Dog's overriding version runs, not the Animal version [1].
Q6State one advantage of using inheritance in OOP, with an example to support your answer.[2]
✅ Mark scheme
Any one with example: Code reuse — the subclass inherits all public methods of the superclass without rewriting them; e.g. Dog inherits GetName() and Describe() from Animal [2]; Extensibility — new subclasses can be added that extend the superclass without modifying existing code; e.g. Cat and Dog can both inherit from Animal [2].
Q7A software team is developing a new school management system. Explain why the waterfall model would be unsuitable for this project, and describe how the iterative (agile) model would be more appropriate. Include reference to requirements gathering, prototyping, and stakeholder feedback.[5]
✅ Mark scheme
Waterfall: requirements must be fixed at start, difficult to change later — 1 mark; unsuitable because school requirements often change as stakeholders see early versions — 1 mark; Iterative: develops in sprints/cycles, delivers working prototypes early — 1 mark; stakeholders review each prototype and feed back changes before next cycle — 1 mark; requirements can be refined throughout, reducing risk of final product mismatch — 1 mark.
Q8Describe the purpose of each of the following activities in the software development lifecycle: (i) requirements analysis, (ii) design, (iii) maintenance. For each, state one specific output or deliverable produced.[6]
✅ Mark scheme
Requirements analysis: defines what the system must do based on stakeholder needs — 1 mark; deliverable: requirements specification document — 1 mark; Design: determines how the system will be built — architecture, algorithms, data structures — 1 mark; deliverable: system design document / UML diagrams / structure charts — 1 mark; Maintenance: fixes bugs, adds features, adapts to new environments after deployment — 1 mark; deliverable: updated software versions / patch notes / change log — 1 mark.
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 — 2.5.2 OOP

10 questions · 10 marks · 10 minutes

← 2.5.1 Programming Paradigms
49 of 82 · Cambridge 9618
2.5.3 Functional & Declarative →