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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Classes and objects

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())           # 70

Access modifiers

Access modifiers control which code can see an attribute or method. UML class diagrams show them with a symbol before the name.

ModifierUML symbolWho can accessPython convention
Public+Any codename
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.

  1. `__balance` is private, so code outside the class cannot set it directly (for example to a negative number).
  2. It can only change through the public method `deposit`, which validates the amount first.
  3. Its value is read through the getter `get_balance`.

Answer: Encapsulation: private data, changed only through validated public methods.

Key idea: Class = blueprint; object = instance made by the constructor (instantiation). Attributes = data, methods = behaviour. Public +, private -, protected #. Encapsulation hides data behind methods.

Check yourself

  1. 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.

  2. 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.

  3. Which UML symbol shows a private attribute?
    • +
    • -
    • #
    • ~
    Show answer

    -. + public, - private, # protected.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.