All subjects · Computer Science · WCP03 Advanced Principles of Computer Science
Object-oriented programming: classes, objects and encapsulation
Edexcel IAL Computer Science · WCP03 Advanced Principles of Computer Science
Object-oriented programming (OOP) organises a program around objects that bundle data with the code that works on it. This lesson covers abstraction, classes, instantiation, attributes, methods, access modifiers and encapsulation.
Classes and objects
- A class is a blueprint (template) that defines the attributes (data) and methods (behaviour) that objects of that type will have.
- An object is an instance of a class, with its own attribute values. Creating one is instantiation, done by calling the class's constructor (in Python,
__init__). - Abstraction means modelling only the details that matter for the problem: a
Studentclass in a school system needs a name and a form, not a shoe size.
class Account:
def __init__(self, owner, balance):
self.owner = owner # public attribute
self.__balance = balance # private attribute
def deposit(self, amount): # public method
if amount > 0:
self.__balance += amount
def get_balance(self): # getter
return self.__balance
acc = Account("Sam", 50) # instantiation
acc.deposit(20)
print(acc.get_balance()) # 70Access modifiers
Access modifiers control which code can see an attribute or method. UML class diagrams show them with a symbol before the name.
| Modifier | UML symbol | Who can access | Python convention |
|---|---|---|---|
| Public | + | Any code | name |
| Private | - | Only the class itself | __name (double underscore) |
| Protected | # | The class and its subclasses | _name (single underscore) |
Encapsulation
Encapsulation bundles attributes and the methods that use them inside one class and hides the internal data (private attributes), so other code can only change it through the class's public methods (getters and setters).
Benefits: data cannot be put into an invalid state (deposit refuses negative amounts); the internal design can change without breaking code that uses the class; easier debugging, because only the class's own methods can change its data.
Why OOP?
Benefits: models real-world things naturally; reuse through inheritance and composition; modularity, so teams work on separate classes; encapsulation protects data; easier maintenance of large programs.
Drawbacks: more code and planning for small tasks; can be slower and use more memory; deep class hierarchies become hard to follow; not ideal for every problem (for example, simple scripts or heavily parallel data processing).
Worked example
In the Account class, explain why balance is private and how its value is changed.
- `__balance` is private, so code outside the class cannot set it directly (for example to a negative number).
- It can only change through the public method `deposit`, which validates the amount first.
- Its value is read through the getter `get_balance`.
Answer: Encapsulation: private data, changed only through validated public methods.
Check yourself
- What is a class in OOP?
- A single variable holding one value
- A blueprint that defines the attributes and methods of a type of object
- An instance of an object
- A loop that repeats code
Show answer
A blueprint that defines the attributes and methods of a type of object. Objects are created from classes.
- What is instantiation?
- Hiding data inside a class
- Inheriting methods from a parent
- Overriding a method
- Creating an object from a class
Show answer
Creating an object from a class. acc = Account('Sam', 50) instantiates the Account class.
- Which UML symbol shows a private attribute?
- +
- -
- #
- ~
Show answer
-. + public, - private, # protected.
- Which code can access a protected attribute?
- Any code anywhere
- Only code in other classes
- The class itself and its subclasses
- Only the main program
Show answer
The class itself and its subclasses. Protected is visible to the class and classes that inherit from it.
- What is encapsulation?
- Bundling data with the methods that use it and hiding the data so it is only changed through those methods
- Creating many objects from one class
- Writing functions with no side effects
- Translating code into machine code
Show answer
Bundling data with the methods that use it and hiding the data so it is only changed through those methods. Private attributes accessed through public getters and setters.
- What is abstraction in OOP?
- Making every attribute public
- Copying code from one class to another
- Including only the details relevant to the problem in a class
- Running methods in parallel
Show answer
Including only the details relevant to the problem in a class. Abstraction removes unnecessary detail.
- Why might a class provide a setter method instead of a public attribute?
- The setter can check that new values are valid before storing them
- Setters run faster than attributes
- Public attributes cannot be read
- Setters make attributes protected
Show answer
The setter can check that new values are valid before storing them. Validation protects the object's state.
Sources
- Pearson Edexcel International Advanced Subsidiary and Advanced Level in Computer Science specification (2026), Issue 1
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 paper, resource booklet and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.