IB Computer Science HL

B.3 Object oriented programming

Model state with objects

Create classes, initialize instances, and maintain an object's rules.

Examples use Python. HL includes additional lessons beyond the SL path.

An object combines state and behavior

A class describes the attributes and operations of a kind of object. An instance holds particular state. Two Account objects may use the same methods but have different balances. The initializer sets each object's starting state.

The deposit operation below rejects non-positive amounts. Keeping validation with the operation makes it easier to preserve the rule that deposits increase the balance. In Python, the leading underscore marks an attribute as non-public by convention rather than enforcing access protection.

class Account:
    def __init__(self):
        self._balance = 0

    def deposit(self, amount):
        if amount > 0:
            self._balance += amount

    def balance(self):
        return self._balance

account = Account()
account.deposit(20)
print(account.balance())

Choose responsibilities carefully

A booking object might describe a reservation while a booking service checks availability across reservations. Assigning every task to one large class makes it harder to explain, test, and change the solution. Use relationships between objects to reflect the model.

Test object state before and after each operation, including rejected operations. Check that updating one instance does not unexpectedly alter another. A class diagram should communicate responsibilities and relationships, not simply reproduce every line of code.

Check yourself

If two accounts are created, should depositing into one update both?Show answer

No. Each account has its own instance state. Shared state would need a deliberate design justification.

References used to build these lessons

We used these official documents as references when building this course's lessons. They are the sources for the syllabus structure and course requirements.

International Baccalaureate · First assessment 2027

This PDF is the public IB subject brief. The complete subject guide and assessment requirements are available through your school or the IB Programme Resource Centre.