20.1 Introduction to OOP
You already organise the world into things. A phone is a thing: it has data (a model, a battery level) and it does jobs (ring, take a photo). A student is a thing too: they have a name and a grade, and they do things like move up a year. Object-Oriented Programming (OOP) lets you write programs the same way — by building “things” called objects that bundle their data and their actions together in one place.
Before OOP, you stored a student's data in separate variables and wrote loose functions that hoped to be passed the right ones. OOP fixes that by keeping each object's data and the actions that work on it locked together. This makes programs easier to read, reuse and maintain — and it is exactly what Paper 4 asks you to design and write.
By the end of this page you will be able to explain the difference between a class and an object, write a class with a constructor and attributes, add methods that change an object's data, and create objects and use them in a main program — the four skills every Paper 4 OOP question is built from.
- The core OOP terminology and how to build and use a simple class with public attributes and methods.
- Encapsulation, private attributes, getters and setters are covered in OOP Part 2.
- Containment (aggregation) is on the OOP Containment page.
- Inheritance and polymorphism are in OOP Part 4.
| Term | Meaning |
|---|---|
| Class | A blueprint (template) that describes what data an object will hold and what it can do. Like an architect’s plan for a house. |
| Object | A real “thing” built from a class, with its own actual values. Like one finished house built from the plan. |
| Instance | Another word for an object. Each object is an instance of its class — one house is an instance of the house plan. |
| Attribute (property) | A piece of data stored inside an object, e.g. a student’s name or grade. |
| Method | An action an object can perform, written as a function inside the class, e.g. promote(). |
| Constructor | A special method that runs automatically when an object is created and sets up its starting attributes. In Python it is __init__. |
| self | Inside a class, self means “this particular object”. It lets a method reach the object’s own attributes. |
| Instantiation | The act of creating an object from a class. When you write x = Student(...) you are instantiating. |
20.1.1 Classes and Objects — the Blueprint Idea
- Before you write any code, you must be crystal clear on one idea, because every other OOP topic depends on it.
- A class is a plan, and an object is a thing made from that plan.
- Mix these two up and your exam answers — and your code — fall apart.
- Examiner focus: “Outline the structure of a class” appeared in
9618/32 O/N 2024 Q10(a)[3]. - “Give three differences between an object and a class” appeared in the same question
Q10(b)[3]. - Plain theory marks are sitting here for anyone who can describe a class and separate it from an object.
Think about a printed registration form. The blank form is the plan — it has spaces for a name and a grade, but it holds no real student. When you photocopy it and write “Musarrat, Grade 11” on one copy and “Aymaan, Grade 12” on another, each filled-in copy is a real thing with its own values.
In OOP, the blank form is the class. Each filled-in copy is an object (also called an instance). You define the class once, then you can create as many objects from it as you like, and each object keeps its own separate data.
| Class | Object |
|---|---|
| A template / blueprint — written once. | A specific thing created from the class — you can create many. |
| Defines what attributes and methods exist. | Holds actual values for those attributes. |
| No memory is set aside for data when the class is defined. | Memory is allocated when the object is created. |
- Step 1 — find the “thing”: the repeated noun the record is about.
- Step 2 — find the data it has (“has a name and an age”).
- Step 3 — find what it does (“book the patient in”).
- The “thing” is the repeated noun; “has” points to attributes, the action points to a method.
- If it is data the car has → attribute. If it is something the car does → method.
- “has” vs “does”.
- For “differences between an object and a class”, give separate, distinct points — examiners will not reward the same idea twice.
- Strong point: a class is a definition/template while an object is an instance with actual values.
- Strong point: a class is written once while many objects can be created.
- Strong point: memory is allocated when the object is created, not when the class is defined.
20.1.1 Classes and Objects
20.1.2 Building a Class — the Constructor and Attributes
- An empty class is useless until each object can be given its starting data.
- The constructor is the method that does this: it runs the moment an object is created and copies the values you pass in into the object's own attributes.
- Get the constructor right and the rest of the class falls into place.
- Examiner focus: Writing a class and its constructor is core Paper 4 code.
- In
9618/42 O/N 2021 Q2(a)[5] candidates had to declare a class and write its constructor that takes parameters and sets them to attributes — and in Python, declare each attribute with a comment. - The structure of the class earns the marks, not clever tricks.
A class definition starts with the keyword class followed by the class name (use PascalCase: Student, BankAccount). Everything that belongs to the class is indented underneath it.
Inside the class, the constructor in Python is always named __init__ (two underscores either side). Its first parameter is always self — that word stands for “the object being built right now”. The remaining parameters are the values you hand over when creating the object. Inside the constructor you copy each parameter into an attribute written as self.attributeName.
class Student:
def __init__(self, name, grade):
self.name = name # attribute: stores the student's name
self.grade = grade # attribute: stores the year groupHere is the same idea in Cambridge pseudocode. The pseudocode constructor is a procedure named NEW, and attributes are declared explicitly:
CLASS Student
PUBLIC Name : STRING
PUBLIC Grade : INTEGER
PUBLIC PROCEDURE NEW(GivenName : STRING, GivenGrade : INTEGER)
Name <- GivenName
Grade <- GivenGrade
ENDPROCEDURE
ENDCLASS- In Python the constructor is
__init__(self, ...); in Cambridge pseudocode it is a procedure namedNEW. - Its job is to copy the parameters into the object's attributes using
self.(Python) or direct assignment (pseudocode).
- Step 1 — class header: class Book: and indent everything inside.
- Step 2 — constructor with parameters: def __init__(self, title, author): — self first, then the two values.
- Step 3 — copy into attributes: assign each parameter to a self. attribute and comment it.
- Mirror the Book example exactly; only the class name, parameters and attribute names change.
- Not every attribute needs a parameter — balance sensibly starts at 0.
- One parameter, one fixed starting value.
- If you write in Python, the syllabus requires you to declare each attribute with a comment (e.g.
# attribute: book title) so the examiner can see your design. - Pseudocode declares them with
DECLARE/type lines instead. - Forgetting the comments in a Python answer can cost you the design mark.
20.1.2 Constructor and Attributes
20.1.3 Adding Methods — Giving Objects Behaviour
- Attributes are only half of an object — they are the data.
- Methods are the actions that read or change that data.
- Bundling the data and the actions together in one class is the whole point of OOP: the object that holds the grade is the same object that knows how to change the grade.
- Examiner focus: Writing methods that update a property (a setter-style action) and return a property (a getter-style action) is examined directly.
- See
9618/42 O/N 2021 Q2(b)[3], where candidates wrote the get methods of a class. - Methods that change attribute values are equally common.
A method is just a function written inside the class. Like the constructor, its first parameter is self, because the method needs to know which object's attributes it should work on. To reach an attribute from inside a method, you write self.attributeName.
Here is a method that promotes a student by adding 1 to their grade. Notice it reads the current grade with self.grade, adds one, and stores the new value back into the same attribute:
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def promote(self):
self.grade = self.grade + 1 # change this object's grade
def display(self):
print("Name:", self.name)
print("Grade:", self.grade)The pseudocode equivalent uses PUBLIC PROCEDURE for an action that does something, and PUBLIC FUNCTION ... RETURNS for a method that hands a value back:
PUBLIC PROCEDURE Promote()
Grade <- Grade + 1
ENDPROCEDURE
PUBLIC FUNCTION GetGrade() RETURNS INTEGER
RETURN Grade
ENDFUNCTION- Every method's first parameter is
self. - Use
self.attributeinside the method to read or change that object's own data. - A method that returns a value is a function; a method that just acts is a procedure.
- Step 1 — method header with self: def deposit(self, amount): — self first, then the value to add.
- Step 2 — change the attribute: read self.balance, add amount, store it back.
- Same shape as deposit: header with self, then update the attribute.
- A method that hands a value back uses return — this is a “getter”.
- This one changes an attribute, so no return.
- The most common slip is leaving out
self— either in the method header or before the attribute name. - Without
self., Python treats it as a brand-new local variable and the object's real attribute never changes. - Examiners watch for
selfon the parameter list and on every attribute reference.
20.1.3 Methods
20.1.4 Creating and Using Objects (Instances)
- A class on its own does nothing — it is just a plan sitting on a shelf.
- The program only comes alive when you create objects from the class and call their methods.
- In Paper 4 the marks for the main program are awarded specifically for creating the object and using its methods, so this final step is where your design actually earns marks.
- Examiner focus: Paper 4 mark schemes consistently require the main program to create the object and then call the methods on it.
- Code that defines a perfect class but never instantiates it loses the application marks — the examiner needs to see the object in use.
To create an object, write the class name followed by brackets, passing in the values the constructor expects. The constructor runs automatically. This act is called instantiation, and the object you get back is an instance of the class. Once you have the object you reach its attributes with object.attribute and call its methods with object.method().
musarrat = Student("Musarrat", 11) # create (instantiate) an object
musarrat.display() # call a method
musarrat.promote() # change the object's data
musarrat.display() # show the updated data
# Output:
# Name: Musarrat
# Grade: 11
# Name: Musarrat
# Grade: 12The pseudocode equivalent creates the object with NEW and calls methods with the same dot notation:
Musarrat <- NEW Student("Musarrat", 11)
Musarrat.Display()
Musarrat.Promote()
Musarrat.Display()- Creating an object is instantiation; the object is an instance of the class.
- Each instance keeps its own attribute values, so calling
aymaan.promote()changes only Aymaan's grade — not anyone else's.
- Step 1 — instantiate: tarnima = Student("Tarnima", 11).
- Step 2 — use the object: call display(), then promote(), then display() again.
- Instantiate first, then call deposit, then print naib.balance.
- Each instance has its own data — promoting one does not affect the other.
- Separate instances keep separate data.
- A complete Paper 4 OOP answer almost always needs three parts: the class definition, then object creation, then method calls.
- If a question says “test your class”, that is your cue to add the main program — leaving it out forfeits easy application marks.
20.1.4 Creating and Using Objects
20.1B Class-Building Drills
Each drill shows one class solved on the left, then a matching task of the same type on the right. Work through the left, cover it, then attempt the right before tapping Show answer. These are skill drills — no marks attached.
- Constructor stores title and author; describe() prints them with print("The book", self.title, "is written by", self.author).
- Mirror Book; only names change.
- Use object.attribute to read a value from outside the class.
- No method needed; print car1.brand and car1.year.
- deposit reads self.balance, adds amount, stores it back.
- Same shape as deposit; balance starts at 0 with no parameter.
- Increment with + 1; the method takes no extra parameter.
- Increment with + 1; the method takes no extra parameter.
- Toggle a Boolean: check if not self.isBorrowed, then set self.isBorrowed = True.
- Mirror borrowBook, but the Boolean starts the other way round.
- One method changes the status string, one prints everything.
- Same pattern as Order: one method changes the status, one prints everything.
- Accumulate a total with self.totalSteps = self.totalSteps + steps, then display all attributes.
- This is the exact twin of FitnessTracker; only the words change.
def promote(self):
grade = grade + 1 # the grade never updates — why?- Python treats grade as a brand-new local variable, so the object's real attribute is never read or changed (and the line raises an error).
- Fix: put self. on both sides.
20.1C Full Exam-Style Question
- The canteen at a school runs a loyalty scheme written using object-oriented programming.
- Each member has a card that stores the holder's name and a number of reward points.
(a) State two differences between an object and a class. [2]
(b)(i) Write program code to declare a class LoyaltyCard with a constructor that takes the holder's name as a parameter and sets the reward points to 0. Use public attributes and, if writing in Python, declare each attribute with a comment. [4]
(b)(ii) Write a method addPoints(amount) that increases the reward points by the given amount. [3]
(c) Write a main program that creates a loyalty card for “Tarnima”, adds 50 points, then outputs the holder's name and points. [3]
- (a) two distinct differences (template vs instance; defined once vs many created; memory on creation).
- (b)(i) class header, constructor with self + holderName, self.holderName set, self.points = 0 (with comments).
- (b)(ii) method header with self + amount, read self.points, store updated value back.
- (c) create object with "Tarnima", call addPoints(50), output name and points.
20.1.5 The Full OOP Vocabulary
- The very first OOP objective in the syllabus is pure terminology, and examiners reward you for defining and matching these words precisely — even before you can write the code for all of them.
- Treat this table as your one-stop glossary for the whole OOP chapter.
- Examiner focus: Defining encapsulation, getter and setter was worth 3 marks in
9618/32 O/N 2022 Q10(a). - Matching OOP terms to their descriptions was worth 4 marks in
9618/32 M/J 2023 Q4. - Knowing the vocabulary cold is free marks.
| Term | Plain-English meaning | Where you use it |
|---|---|---|
| Class | A blueprint that defines what attributes and methods an object will have. | This page |
| Object / Instance | A specific thing built from a class, holding its own real values. | This page |
| Attribute (property) | A piece of data stored inside an object. | This page |
| Method | An action — a function — written inside a class. | This page |
| Constructor | A special method that sets up a new object automatically (NEW in pseudocode, __init__ in Python). | This page |
| Encapsulation | Bundling data and the methods that work on it inside one class, and hiding the data from outside code. | OOP Part 2 |
| Getter | A method that returns the value of an attribute. | OOP Part 2 |
| Setter | A method that updates the value of an attribute. | OOP Part 2 |
| Public / Private | Access modifiers. Public members are reachable anywhere; private members are usable only inside the class (PRIVATE in pseudocode, a __ prefix in Python). | OOP Part 2 |
| Inheritance | A child (sub) class reuses the attributes and methods of a parent (super) class (INHERITS in pseudocode, class Child(Parent) in Python). | OOP Part 4 |
| Polymorphism | The same method name behaves differently depending on the object’s class (method overriding). | OOP Part 4 |
| Containment (aggregation) | An object holds other objects as its attributes — e.g. an Invoice that contains several Order objects. | OOP Containment |
- Public members can be accessed from anywhere; private members can be used only inside their own class — written as
PRIVATEin pseudocode or with a__prefix in Python. - Hiding attributes as private and reaching them only through getters and setters is what encapsulation means in practice (you build exactly this in Part 2).
20.1.5 OOP Vocabulary
✓ Key Points Summary
20.1D Practice Tasks
Fifteen exam-style tasks. Click Hint for bullet-point guidance, then Help to reveal a worked Python solution.
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
A class is best described as:
Question 2Multiple Choice
In Python, the constructor method is named:
Question 3True / False
Every method's first parameter is self.
Question 4Multiple Choice
What does self.attributeName do inside a method?
Question 5Multiple Choice
How do you instantiate an object in Python?
Question 6Multiple Choice
A method that returns the value of an attribute is a:
Question 7Multiple Choice
Which is a difference between a class and an object?
Question 8True / False
In a Python OOP answer, you must declare each attribute with a comment.
Question 9Multiple Choice
What is encapsulation?
Question 10Multiple Choice
In pseudocode, the constructor is a procedure named:
Question 11Multiple Choice
What is inheritance?
Question 12Multiple Choice
A complete Paper 4 OOP answer needs:
Answer all 12 questions to enable submission.