📄 Paper 1 · 4.1 Fundamentals of Programming
⭐ Pro
4.1.2b OOP — Classes, Objects & Encapsulation
AQA 7517 · A-Level Computer Science · ~20 min read

Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that organises code around objects — entities that combine data (attributes) and behaviour (methods). OOP models real-world entities, making complex systems easier to design and maintain.

Classes and Objects

Class

A class is a blueprint or template that defines the attributes and methods that all objects of that type will have. A class defines the structure but does not represent a specific instance.

Object

An object is a specific instance of a class. Multiple objects can be created from the same class, each with their own unique attribute values.

// AQA pseudocode class definition
CLASS Animal
    PRIVATE name : STRING
    PRIVATE species : STRING
    PRIVATE age : INTEGER

    PUBLIC PROCEDURE NEW(n : STRING, s : STRING, a : INTEGER)
        name ← n
        species ← s
        age ← a
    ENDPROCEDURE

    PUBLIC FUNCTION getName() RETURNS STRING
        RETURN name
    ENDFUNCTION

    PUBLIC PROCEDURE setAge(a : INTEGER)
        age ← a
    ENDPROCEDURE

    PUBLIC PROCEDURE speak()
        OUTPUT name & " makes a sound."
    ENDPROCEDURE
ENDCLASS

// Creating objects (instances)
myDog ← NEW Animal("Rex", "Dog", 3)
myCat ← NEW Animal("Luna", "Cat", 2)
OUTPUT myDog.getName()   // Output: Rex

Attributes and Methods

TermDescription
AttributeA variable belonging to a class that stores data about the object (also called instance variable or field)
MethodA function or procedure defined within a class that defines the object's behaviour
Constructor (NEW)A special method called when an object is created; initialises the object's attributes. In AQA pseudocode, this is PROCEDURE NEW()

Encapsulation

Encapsulation is one of the four pillars of OOP. It is the bundling of data (attributes) and methods that operate on that data within a single class, while restricting direct access to the internal data from outside the class.

Access Modifiers

ModifierAQA keywordAccessible from
PrivatePRIVATEOnly within the class itself
PublicPUBLICFrom anywhere — inside and outside the class

Getters and Setters

Since attributes are typically PRIVATE, access is provided through getter and setter methods:

  • Getter (accessor) — a PUBLIC method that returns the value of a private attribute (e.g. getName())
  • Setter (mutator) — a PUBLIC method that sets/updates the value of a private attribute, often with validation (e.g. setAge())
PUBLIC FUNCTION getAge() RETURNS INTEGER
    RETURN age
ENDFUNCTION

PUBLIC PROCEDURE setAge(a : INTEGER)
    IF a >= 0 THEN
        age ← a
    ENDIF
ENDPROCEDURE

Benefits of Encapsulation

  • Data hiding / information hiding — internal implementation is hidden from external code
  • Control over data — setters can validate data before updating attributes
  • Reduced coupling — code outside the class doesn't depend on internal implementation details
  • Easier maintenance — internal implementation can change without affecting external code (as long as the interface remains the same)

Instantiation

Instantiation is the process of creating an object from a class using the constructor (NEW). Each object is an independent instance with its own copy of the attributes.

Exam tip: AQA commonly asks: define a class in pseudocode with PRIVATE attributes and PUBLIC methods including a constructor; explain encapsulation and give benefits; distinguish class from object. Know the AQA keywords: CLASS … ENDCLASS, PRIVATE, PUBLIC, PROCEDURE NEW() for constructors, and the dot notation (myDog.getName()) for calling methods.
⚠️ Common Mistakes
  • Confusing class and object — a class is a template; an object is a specific instance created from it
  • Thinking encapsulation just means making things private — it's also about providing controlled public access via getters/setters
  • Using FUNCTION for a constructor — in AQA pseudocode the constructor is PROCEDURE NEW()
  • Forgetting that you use NEW to create an instance: myObj ← NEW ClassName()
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.1.2b OOP Classes, Objects & Encapsulation

8 questions · instantly marked · AQA 7517 standard

Q1Explain the difference between a class and an object. Give a real-world example of each.[4]
✅ Mark scheme
Mark scheme
A class is a blueprint/template that defines the attributes and methods for a type of object [1]; an object is a specific instance of a class with its own attribute values [1]; class example: Animal/Car/Person [1]; object example: myDog/myCar/john [1].
Q2What is encapsulation? Give two benefits of using it.[4]
✅ Mark scheme
Mark scheme
Encapsulation is bundling attributes and methods together in a class and restricting direct external access to the internal data [1]; benefits (any two): data hiding/information hiding [1]; controlled access via getters/setters with validation [1]; reduced coupling between classes [1]; easier maintenance — internal changes don't break external code [1].
Q3State the difference between a getter method and a setter method. Why are attributes typically declared as PRIVATE?[3]
✅ Mark scheme
Mark scheme
A getter returns the value of a private attribute [1]; a setter sets/updates the value of a private attribute (often with validation) [1]; attributes are private to prevent external code directly accessing or modifying the object's internal data, protecting data integrity [1].
Q4Write an AQA pseudocode class called BankAccount with: private attributes balance (REAL) and accountNumber (STRING); a constructor that sets both; a getter for balance; a method deposit(amount) that adds to balance if amount > 0.[6]
✅ Mark scheme
Mark scheme
CLASS BankAccount [1]; PRIVATE balance : REAL; PRIVATE accountNumber : STRING [1]; PUBLIC PROCEDURE NEW(acc : STRING, bal : REAL); accountNumber ← acc; balance ← bal; ENDPROCEDURE [1]; PUBLIC FUNCTION getBalance() RETURNS REAL; RETURN balance; ENDFUNCTION [1]; PUBLIC PROCEDURE deposit(amount : REAL); IF amount > 0 THEN balance ← balance + amount; ENDIF; ENDPROCEDURE [1]; ENDCLASS [1].
Q5A Car class has a private attribute speed. A setter setSpeed(s) should only allow s between 0 and 200. Write this setter in AQA pseudocode.[3]
✅ Mark scheme
Mark scheme
PUBLIC PROCEDURE setSpeed(s : INTEGER) [1]; IF s >= 0 AND s <= 200 THEN speed ← s [1]; ENDIF; ENDPROCEDURE [1].
Q6What is the purpose of a constructor (PROCEDURE NEW) in a class? What happens when you call NEW?[2]
✅ Mark scheme
Mark scheme
A constructor initialises a new object's attributes when it is created [1]; calling NEW creates a new instance (object) of the class and runs the constructor to set up its initial state [1].
Q7How is an object's method called in AQA pseudocode? Give an example using a Car object called myCar with a method accelerate().[2]
✅ Mark scheme
Mark scheme
Using dot notation: objectName.methodName() [1]; example: CALL myCar.accelerate() or myCar.accelerate() [1].
Q8Explain why it is good practice to include data validation inside a setter method rather than in the main program.[2]
✅ Mark scheme
Mark scheme
Validation inside the setter ensures no invalid values can be assigned regardless of which part of the program calls it [1]; this protects data integrity and means the validation only needs to be written once, rather than repeated everywhere the attribute might be set [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 12
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — OOP Classes & Encapsulation

10 questions · 10 minutes

← 4.1.2a Procedural Programming
9 of 70 · AQA 7517
4.1.2c OOP Inheritance & Polymorphism →