Pro Content

Upgrade to access all Cambridge 9618 lessons including OOP fundamentals, classes, encapsulation, getters, setters and UML design.

Upgrade to Pro →
← Back to Dashboard
📗 Paper 4 · 4.1 Further Programming
4.1.2 Object-Oriented Programming — Fundamentals
Cambridge 9618 · International A Level Computer Science · ~18 min read
Notes
Video
Slides
Quiz
Worksheet

Classes and Objects

Object-Oriented Programming (OOP) organises code around objects — self-contained units that combine data (attributes) and behaviour (methods). It reflects how we think about real-world things.

📄 Class
A blueprint or template that defines what attributes and methods all objects of that type will have. A class is a definition — it occupies no runtime memory until objects are created from it. Example: the BankAccount class defines what a bank account looks like and what it can do.
🏦 Object (Instance)
A specific instance created from a class. Each object has its own copy of the attributes, but shares the class's method definitions. Many objects can be created from one class — each with different attribute values. Example: alice_account and bob_account are two objects of the BankAccount class.

Attributes and Methods

  • Attributes (also called fields, properties, or instance variables): the DATA stored in an object. Each object has its own copy. Example: accountNumber, balance, holderName.
  • Methods: the BEHAVIOURS or OPERATIONS an object can perform — functions defined inside the class. Methods operate on the object's own data. Example: deposit(), withdraw(), getBalance().

BankAccount class in pseudocode

CLASS BankAccount
  PRIVATE accountNumber : STRING
  PRIVATE balance : REAL
  PRIVATE holderName : STRING

  PUBLIC PROCEDURE NEW(accNum, name, initialBalance)  // Constructor
    accountNumberaccNum
    holderNamename
    balanceinitialBalance
  ENDPROCEDURE

  PUBLIC PROCEDURE deposit(amount : REAL)
    IF amount > 0 THEN
      balancebalance + amount
    ENDIF
  ENDPROCEDURE

  PUBLIC FUNCTION getBalance() RETURNS REAL  // Getter
    RETURN balance
  ENDFUNCTION
ENDCLASS

// Creating objects (instances)
myAccountNEW BankAccount("ACC001", "Alice", 500.00)
myAccount.deposit(200.00)
OUTPUT myAccount.getBalance()  // Outputs 700.00

Encapsulation

Encapsulation means bundling data (attributes) and the methods that operate on that data together in a single unit (the class), and hiding the internal state from outside code. External code interacts with the object only through its public interface (public methods).

Benefits of encapsulation:

  • Data protection: attributes are private — external code cannot directly modify them in invalid ways (e.g. setting balance to a negative number)
  • Maintainability: the internal implementation can change without affecting external code — as long as the public interface stays the same
  • Modularity: each class is self-contained and reusable
  • Abstraction: users of the class don't need to know HOW it works internally — just what methods are available

Access Modifiers

ModifierAccessible from…Symbol in UMLUse case
PUBLICAnywhere — inside the class, from other classes, and externally+Methods that form the public interface; constructors
PRIVATEOnly inside the class itself — NOT accessible from outside or subclassesAll attributes; internal helper methods
PROTECTEDInside the class AND its subclasses (inherited classes) — not from unrelated external code#Attributes/methods needed by subclasses during inheritance

Best practice: attributes should almost always be PRIVATE. Access is provided through public getter (accessor) and setter (mutator) methods.

Getters and Setters

Since attributes are private, getter methods allow external code to READ an attribute value, and setter methods allow external code to WRITE/UPDATE an attribute value — with built-in validation.

// Getter — reads the private attribute
PUBLIC FUNCTION getBalance() RETURNS REAL
  RETURN balance
ENDFUNCTION

// Setter — validates before writing
PUBLIC PROCEDURE setBalance(newBalance : REAL)
  IF newBalance >= 0 THEN   // validation!
    balancenewBalance
  ENDIF
ENDPROCEDURE

Without setters, if balance were public, any code could write myAccount.balance ← -999999 — breaking the object's integrity. With a private attribute and setter, the class CONTROLS what values are acceptable.

Constructor

A constructor is a special method that is automatically called when an object is created (instantiated). Its purpose is to initialise the object's attributes to valid starting values. In Cambridge pseudocode, the constructor is written as PROCEDURE NEW(...).

UML Class Diagram

A UML (Unified Modelling Language) class diagram shows the structure of a class: its name, attributes, and methods, with access modifiers.

BankAccount
accountNumber : String
balance : Real
holderName : String
+ NEW(accNum, name, balance)
+ deposit(amount : Real)
+ withdraw(amount : Real)
+ getBalance() : Real
+ getAccountNumber() : String

Convention: three sections separated by horizontal lines — class name (top), attributes (middle), methods (bottom). + = public, = private, # = protected.

The Four Pillars of OOP

🔒 Encapsulation
Bundling data and methods together; hiding internal state; exposing only a public interface. Covered this lesson.
🎭 Abstraction
Showing only relevant details to the outside; hiding complexity. Users call deposit() without knowing how the balance is managed internally.
🧬 Inheritance
A subclass inherits attributes and methods from a parent class, extending or overriding them. Covered in lesson 4.1.3.
🔀 Polymorphism
The same method name works differently on different types. A Shape object's area() works differently for Circle vs Rectangle. Covered in lesson 4.1.3.
Cambridge 9618 exam tip: Be able to write a class definition in pseudocode with correct PRIVATE attributes, PUBLIC constructor (NEW), and PUBLIC getters/setters. Draw UML class diagrams with correct sections and symbols (+ public, − private, # protected). Explain encapsulation — private attributes + public methods + validation in setters. Distinguish class (blueprint, one) from object (instance, many). The constructor is called when an object is created with NEW; it initialises attributes. Common exam question: "why should attributes be declared as PRIVATE?" — answer: encapsulation, data protection, external code cannot directly set invalid values.
⚠️ Common Mistakes
  • Making attributes PUBLIC instead of PRIVATE — this breaks encapsulation. Attributes should be private; access via getter/setter methods.
  • Confusing class and object — a class is a BLUEPRINT (one definition); objects are INSTANCES (many can be created). "BankAccount is a class; alice_account is an object of type BankAccount."
  • Forgetting the constructor's purpose — the constructor initialises attributes when an object is created. It does NOT have a return type. In Cambridge pseudocode: PROCEDURE NEW(...).
  • Getting UML symbols wrong — + is PUBLIC (not private), − is PRIVATE (not public). A common mix-up in exams.
  • Saying encapsulation just means "hiding data" — encapsulation means BUNDLING data and methods together AND controlling access through a public interface. The hiding is a consequence, not the full definition.
  • Forgetting validation in setters — a setter without validation gives no benefit over a public attribute. The point of setters is to enforce rules (e.g. balance ≥ 0) before modifying the attribute.
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 4.1.2 OOP Fundamentals

8 questions · Cambridge 9618 standard

Q1Distinguish between a class and an object in object-oriented programming.[2]
✅ Mark scheme
A class is a blueprint or template that defines the attributes and methods for a type of object — it is a definition, not a specific thing [1]; an object is an instance created from a class — it is a specific, individual entity with its own values for the attributes defined by the class. Many objects can be created from one class [1].
Q2Explain what encapsulation means in OOP. Give one benefit of using encapsulation.[3]
✅ Mark scheme
Encapsulation means bundling data (attributes) and the methods that operate on that data together in a single class [1]; and controlling access to the internal state by making attributes private and providing public methods (getters/setters) as the only means of interaction [1]; one benefit: data protection — external code cannot directly set attributes to invalid values, as the setter can validate input before modifying the attribute [1]. Accept: maintainability (implementation can change without affecting external code), modularity, abstraction.
Q3Write pseudocode for a class called Student with private attributes name and mark. Include a constructor, a getter for mark, and a setter for mark that only accepts values between 0 and 100.[6]
✅ Mark scheme
CLASS Student [1]; PRIVATE name : STRING and PRIVATE mark : INTEGER declared [1]; PUBLIC PROCEDURE NEW(studentName, studentMark) — constructor with correct syntax [1]; attributes correctly initialised in constructor: name ← studentName, mark ← studentMark [1]; PUBLIC FUNCTION getMark() RETURNS INTEGER — getter correctly returns mark [1]; PUBLIC PROCEDURE setMark(newMark) — setter validates: IF newMark >= 0 AND newMark <= 100 THEN mark ← newMark ENDIF — correct validation [1].
Q4Draw a UML class diagram for a class called Car with private attributes: make (String), model (String), speed (Real); and public methods: accelerate(amount:Real), brake(amount:Real), getSpeed():Real.[3]
✅ Mark scheme
Three sections: top section = class name "Car" [1]; middle section = attributes: − make : String, − model : String, − speed : Real (all with − prefix for private) [1]; bottom section = methods: + accelerate(amount:Real), + brake(amount:Real), + getSpeed():Real (all with + prefix for public) [1].
Q5Why should attributes in a class normally be declared as PRIVATE rather than PUBLIC?[2]
✅ Mark scheme
If attributes are PUBLIC, external code can directly read and modify them to any value — invalid or inconsistent data could be written without any checks [1]; making attributes PRIVATE enforces encapsulation — access is only possible through public getter and setter methods, which can validate input and maintain the object's integrity [1].
Q6Name and briefly explain the four pillars of object-oriented programming.[4]
✅ Mark scheme
Encapsulation: bundling data and methods together; hiding internal state and exposing only a public interface [1]; Abstraction: hiding complexity; exposing only what is necessary — users interact with simplified interfaces without needing to know internal implementation [1]; Inheritance: a subclass inherits the attributes and methods of a parent class, extending or overriding them — promotes code reuse [1]; Polymorphism: the same method name behaves differently depending on the object type — e.g. area() returns a different result for Circle vs Rectangle [1].
Q7Write a BankAccount class in Cambridge 9618 pseudocode with: PRIVATE attributes accountNumber (STRING) and balance (REAL); a PUBLIC constructor taking accountNumber and an openingBalance; a PUBLIC PROCEDURE Deposit(amount : REAL) that adds amount to balance; a PUBLIC FUNCTION GetBalance() RETURNS REAL.[5]
✅ Mark scheme
CLASS BankAccount [1]; PRIVATE accountNumber : STRING; PRIVATE balance : REAL [1]; PUBLIC PROCEDURE NEW(num:STRING, opening:REAL) accountNumber←num; balance←opening ENDPROCEDURE [1]; PUBLIC PROCEDURE Deposit(amount:REAL) balance←balance+amount ENDPROCEDURE [1]; PUBLIC FUNCTION GetBalance() RETURNS REAL; RETURN balance ENDFUNCTION ENDCLASS [1].
Q8Explain why balance is declared PRIVATE rather than PUBLIC. Then explain what ENCAPSULATION means and state TWO benefits it provides.[4]
✅ Mark scheme
PRIVATE prevents external code from directly reading/modifying balance — it can only be changed through authorised methods like Deposit() [1]; Encapsulation bundles data and the methods that operate on it into one unit, hiding internal implementation details from outside code [1]; Benefit 1: prevents invalid direct assignments (e.g. setting balance to a negative value bypassing business rules) [1]; Benefit 2: internal representation can change (e.g. storing balance in pence as INTEGER) without breaking external code, since only the internal methods need updating [1].
Topic Quiz
Question 1 of 10
You scored
out of 10
Card 1 of 9
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 4.1.2 OOP Fundamentals

10 questions · 10 marks · 10 minutes

← 4.1.1 Recursion
66 of 82 · Cambridge 9618
4.1.3 OOP Inheritance →