Skip to content

Object-Oriented Programming

1.2.4 Types of programming language · Paper 1 · 8 flashcards · 2 questions

Object-Oriented Programming is a paradigm that organises code around objects - entities that combine data and behaviour.

Definition
Object-Oriented Programming (OOP) is a paradigm based on the concept of objects, which contain both data (attributes) and code (methods) that operate on that data.

Classes and Objects

Definition
A class is a blueprint or template that defines what attributes and methods objects of that type will have.

An object is a specific instance of a class - a real entity created from the blueprint.
Worked Example: Class and Object
pseudocode
// CLASS - the blueprint
class Dog:
    // Attributes (data)
    private name
    private breed
    private age
    
    // Constructor - creates new objects
    public procedure new(givenName, givenBreed, givenAge):
        name = givenName
        breed = givenBreed
        age = givenAge
    end procedure
    
    // Methods (behaviour)
    public procedure bark():
        print(name + " says: Woof!")
    end procedure
    
    public function getAge():
        return age
    end function
end class

// OBJECTS - instances of the class
myDog = new Dog("Rex", "German Shepherd", 3)
yourDog = new Dog("Bella", "Labrador", 5)

// Using the objects
myDog.bark()             // Output: Rex says: Woof!
print(yourDog.getAge())  // Output: 5

Encapsulation

Definition
Encapsulation is the bundling of data (attributes) and methods that operate on that data within a single unit (class), while restricting direct access to some components.
Key Point
Public vs Private:
• Private attributes/methods: Only accessible within the class
• Public attributes/methods: Accessible from outside the class

Why Encapsulate?
• Protects data from invalid modifications
• Hides implementation details
• Makes code easier to maintain
• Provides controlled access through methods (getters/setters)
Encapsulation Example:
pseudocode
class BankAccount:
    private balance    // Cannot be accessed directly from outside
    
    public procedure deposit(amount):
        if amount > 0 then
            balance = balance + amount
        end if
    end procedure
    
    public function getBalance():
        return balance
    end function
end class

// Usage
account = new BankAccount()
// account.balance = 1000000    // ERROR - balance is private!
account.deposit(500)            // OK - uses public method
print(account.getBalance())     // OK - uses public method

Inheritance

Definition
Inheritance allows a new class (subclass/child) to inherit attributes and methods from an existing class (superclass/parent), promoting code reuse.
Worked Example: Inheritance
pseudocode
// SUPERCLASS (parent)
class Animal:
    private name
    private age

    // Constructor
    public procedure new(givenName, givenAge):
        name = givenName
        age = givenAge
    end procedure

    public function getName():
        return name
    end function

    public procedure makeSound():
        print(getName() + " makes a sound")
    end procedure
end class

// SUBCLASS (child) - inherits from Animal
class Dog inherits Animal:
    private breed

    // Dog has an extra attribute, so it has its own constructor
    public procedure new(givenName, givenAge, givenBreed):
        super.new(givenName, givenAge)    // Run the constructor of Animal
        breed = givenBreed
    end procedure

    // Override the parent method
    public procedure makeSound():
        print(getName() + " barks: Woof!")
    end procedure

    // A method only Dog has
    public procedure fetch():
        print(getName() + " fetches the ball")
    end procedure
end class

// SUBCLASS (child) - uses the constructor it inherits from Animal
class Cat inherits Animal:
    public procedure makeSound():
        print(getName() + " meows: Meow!")
    end procedure
end class

// Usage
myDog = new Dog("Rex", 3, "German Shepherd")
myDog.makeSound()         // Output: Rex barks: Woof!
myDog.fetch()             // Output: Rex fetches the ball

myCat = new Cat("Whiskers", 5)
myCat.makeSound()         // Output: Whiskers meows: Meow!

// myDog.name = "Max"     // ERROR - name is private to Animal

Polymorphism

Definition
Polymorphism means "many forms" - it allows objects of different classes to be treated as objects of a common parent class, with each responding to methods in their own way.
Polymorphism Example:
pseudocode
// Both Dog and Cat inherit from Animal
animals = [new Dog("Rex", 3, "German Shepherd"), new Cat("Whiskers", 5), new Dog("Bella", 2, "Labrador")]

// Polymorphism - same method call, different behaviour
for each animal in animals do
    animal.makeSound()
end for

// Output:
// Rex barks: Woof!
// Whiskers meows: Meow!
// Bella barks: Woof!

// The code doesn't need to know the specific type -
// each object responds appropriately

OOP Principles Summary

Key Point
Encapsulation
• Description: Bundle data and methods, hide internals
• Benefit: Data protection, maintainability

Inheritance
• Description: Child classes inherit from parent
• Benefit: Code reuse, hierarchy

Polymorphism
• Description: Same interface, different implementations
• Benefit: Flexibility, extensibility

Abstraction
• Description: Hide complex details, show only essentials
• Benefit: Simplicity, focus
Exam Tip
OOP vs Procedural:
• OOP: Data and methods together in objects
• Procedural: Data and procedures separate
• OOP better for: Large projects, code reuse, modelling real-world entities
• Procedural better for: Small scripts, simple programs, learning