COMPUTER SCIENCE CAFÉ
  • WORKBOOKS
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • BLOCKY GAMES
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
  • WORKBOOKS
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • BLOCKY GAMES
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
HOME    >    IB    >    COMPUTATIONAL THINKING
NEXT TOPIC >
OOP THEORY
Picture

COMPUTATIONAL THINKING | OBJECT ORIENTED PROGRAMMING

DESIGNED FOR IB EXAMINATIONS
OBJECTIVES
B3.1.1 Evaluate the fundamentals of OOP
Model real-world entities using OOP concepts: classes, objects, inheritance, encapsulation, polymorphism
The advantages and disadvantages of using OOP in various programming scenarios

  • LEARN
  • CODE
  • QUESTIONS
  • FLASHCARDS
  • WORKBOOK
<
>
Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which are used to represent real-world entities. Each object combines both data (attributes) and behaviour (methods) into a single unit. This approach allows developers to model complex systems in a structured and intuitive way.

At its core, OOP focuses on organizing code around classes and objects rather than simply functions and procedures. A class acts as a blueprint, while an object is an instance of that class. For example, in a school system, a Student class could define attributes such as name and grade, and methods such as calculateAverage().

Why is OOP Used?
OOP is widely used because it helps manage complexity in software development. As programs grow larger, maintaining and modifying code becomes increasingly difficult. OOP addresses this by promoting:
  • Modularity: Code is divided into independent classes, making it easier to develop and debug.
  • Reusability: Classes can be reused in different parts of a program or in new projects.
  • Scalability: Systems can be extended without rewriting existing code.
  • Maintainability: Changes to one part of the program are less likely to affect others.

For example, in a banking system, separate classes such as Account, Customer, and Transaction allow developers to manage each component independently while still interacting as part of a larger system.

When is OOP Appropriate?
OOP is particularly useful when:
  • Modelling real-world systems (e.g. simulations, games, management systems)
  • Building large or complex applications that require clear structure
  • Multiple developers are working on the same project, requiring well-organized code
  • Code needs to be reused or extended over time

However, OOP is not always the best choice. For small programs or tasks that require simple, linear logic, other paradigms such as procedural programming may be more efficient.

SECTION 1 | B3.1.1 FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING (OOP)

Object-oriented programming (OOP) is a paradigm used to design software by modelling real-world entities as objects. These objects are created from classes, which define their structure (attributes) and behaviour (methods). OOP enables developers to organise code in a way that reflects how systems exist and interact in the real world.

Modelling Real-World Entities
One of the key strengths of OOP is its ability to represent real-world systems. For example, consider a Car:
  • A car has attributes such as colour, speed, and fuel level
  • A car has behaviours such as accelerate(), brake(), and refuel()

In OOP, this would be modelled using a class:
  • The class defines the blueprint (Car)
  • Each object represents a specific car (e.g. a red Toyota, a blue Tesla)

This abstraction allows programmers to break down complex systems into manageable components.

Classes and Objects
A class is a template used to define the properties and behaviours of an object. An object is an instance of a class.
  • Class → blueprint
  • Object → instance of the blueprint
Python: Class and Object

    
Encapsulation
Encapsulation is the principle of restricting access to an object's internal data. This is achieved by controlling how attributes are accessed or modified.
  • Protects data from unintended changes
  • Ensures controlled interaction through methods
In Python, this is often indicated using conventions such as _protected or __private attributes.

Inheritance
Inheritance allows a class (child class) to inherit attributes and methods from another class (parent class).
  • Promotes code reuse
  • Supports hierarchical relationships
PYTHON: Inheritance

    
Polymorphism
​
Polymorphism allows methods to behave differently depending on the object that is using them.
  • Same method name, different behaviour
  • Increases flexibility and extensibility
Example:
PYTHON: Polymorphism

    

SECTION 2 | ADVANTAGES AND DISADVANTAGES

Advantages of OOP
OOP provides several benefits when developing software:
  • Modularity | Programs are divided into smaller, manageable classes
  • Reusability | Code can be reused through inheritance and existing class structures
  • Maintainability | Changes can be made to one class without affecting the entire system
  • Scalability | Systems can grow by adding new classes or extending existing ones
  • Real-world modelling | Makes it easier to design systems that reflect real-life scenarios

Disadvantages of OOP
Despite its strengths, OOP is not always the most efficient approach:
  • Increased complexity | Designing classes and relationships can be time-consuming
  • Performance overhead | Object creation and method calls can use more memory and processing time
  • Not ideal for small programs | Simpler paradigms (e.g. procedural programming) may be more efficient
  • Overengineering risk | Developers may create unnecessary classes, making systems harder to understand

OOP is highly effective for solving complex, large scale problems, particularly where systems can be broken down into interacting components. It supports structured design, reuse, and maintainability.

However, OOP is not always the best choice. For smaller or highly algorithmic problems, simpler approaches may be more appropriate. Therefore, the use of OOP should be evaluated based on the problem requirements, rather than applied universally
Modelling Real-World Entities in Python (OOP)

In this practical section, we will begin learning how to write object-oriented programs in Python. We will use a consistent scenario throughout the OOP unit: a game character system. This will help you build understanding step-by-step as concepts become more advanced.

STEP 1 | CREATING YOUR FIRST CLASS
We begin by modelling a simple game character.
A character has:
  • a name
  • a health value

This can be represented using a class.
PYTHON: Creating a class

    
This defines a class called Character.
  • __init__ is the constructor (runs when an object is created)
  • self.name and self.health are attribute

STEP 2 | CREATING OBJECTS (INSTANCES)
Now we create actual characters from the class:
PYTHON: Creating Objects (instances)

    
In the above example each object:
  • Uses the same class
  • Stores different data

STEP 3 | ADDING BEHAVIOUR (METHODS)
Objects are not just data — they can perform actions.

Adding a method:
PYTHON: Adding a Method

    
And to call the method.
PYTHON: Call a Method

    

CHALLENGE 1 | ADD A NEW METHOD

Add a new method called display_info() that prints:
  • Name: Aria, Health: 100

Test it on both hero and enemy.
Need more help? (Sample Answer)

Add a method to the class that prints the character’s details:

class Character:
    def __init__(self, name, health):
        self.name = name
        self.health = health

    # New method to display character information
    def display_info(self):
        print("Name:", self.name, ", Health:", self.health)


# Create objects
hero = Character("Aria", 100)
enemy = Character("Goblin", 50)

# Test the method on both objects
hero.display_info()
enemy.display_info()
      
STEP 4 | EINTRODUCING ENCAPULATION
Encapsulation means protecting data inside the object.

We will make health harder to modify directly:
PYTHON: Encapsulation

    

CHALLENGE 2 | PROTECT DATA

​Modify the take_damage() method so that:
  • Health cannot go below 0
Need more help? (Sample Answer)

Update the method to ensure health never becomes negative:

class Character:
    def __init__(self, name, health):
        self.name = name
        self.__health = health

    def take_damage(self, amount):
        self.__health -= amount

        # Ensure health does not go below 0
        if self.__health < 0:
            self.__health = 0
      

This checks if health has dropped below 0 and resets it to 0 if needed.

STEP 5 | INHERITANCE
​We now create a more specific type of character.
PYTHON: INHERITANCE

    
The Warrior class:
  • Inherits from Character
  • Reuses existing method

CHALLENGE 3 | INHERITANCE

Create a new class called Mage that:
  • Inherits from Character
  • Has a method cast_spell() that prints a message
Need more help? (Sample Answer)

Create a new class that inherits from Character and add a new method:

class Mage(Character):

    # New method specific to Mage
    def cast_spell(self):
        print(self.name + " casts a powerful spell!")


# Example usage
mage = Mage("Luna", 80)
mage.cast_spell()
      

The Mage class inherits all attributes and methods from Character, and adds its own unique behaviour.

STEP 6 | POLYMORPHISM
Different objects can respond to the same method differently.
PYTHON: POLYMORPHISIM

    
​Each object behaves differently even though the same method is called.

CHALLENGE 4 | POLYMORPHISM

Add a new class Archer that:​
  • Overrides attack()
  • Prints a unique attack message
Need more help? (Sample Answer)

Create a new class that inherits from Character and overrides the attack method:

class Archer(Character):

    # Override the attack() method
    def attack(self):
        print(self.name + " shoots an arrow!")


# Example usage
archer = Archer("Robin", 90)
archer.attack()
      

The Archer class reuses the structure of Character but changes how the attack() method behaves.

CHALLENGE 4 | MINI TAKS

Create a small system with:
  • At least 2 different character types
Each must:
  • Have a constructor
  • Have at least 2 methods
  • Override one method (polymorphism)

Then:
  • Store them in a list
  • Loop through them and call their methods
Need more help? (Sample Answer)

This is one possible solution that meets all the requirements:

# Base class
class Character:
    def __init__(self, name, health):
        self.name = name
        self.health = health

    def attack(self):
        print(self.name + " attacks!")

    def display_info(self):
        print("Name:", self.name, ", Health:", self.health)


# First subclass
class Warrior(Character):

    # Override attack method (polymorphism)
    def attack(self):
        print(self.name + " swings a sword!")

    def defend(self):
        print(self.name + " raises a shield!")


# Second subclass
class Mage(Character):

    # Override attack method (polymorphism)
    def attack(self):
        print(self.name + " casts a fireball!")

    def cast_spell(self):
        print(self.name + " casts a powerful spell!")


# Create objects
w = Warrior("Thor", 120)
m = Mage("Luna", 80)

# Store objects in a list
characters = [w, m]

# Loop through and call methods
for c in characters:
    c.display_info()
    c.attack()
      

This example demonstrates:

  • Classes and objects
  • Multiple subclasses (inheritance)
  • Method overriding (polymorphism)
  • Multiple methods per class
  • Using a list to manage objects
Picture

B3.1.1 Check Your Understanding

1. What is a class in OOP?

2. What does encapsulation help to achieve?

3. What is inheritance?

4. What is polymorphism?

5. When is OOP most appropriate?

COMING SOON
Picture
A3 DATABASE FUNDAMENTALS
  
☐  3.1.1 DDL AND DML
Picture
SUGGESTIONS
We would love to hear from you
SUBSCRIBE 
To enjoy more benefits
We hope you find this site useful. If you notice any errors or would like to contribute material then please contact us.