Object-Oriented Programming
Object-oriented programming
Object-oriented programming (OOP) structures a program around objects — self-contained bundles of data (attributes) and behaviour (methods) — rather than around procedures. It models real-world things and is a key A-Level paradigm.
Classes and objects
- A class is a template/blueprint that defines the attributes and methods an object will have.
- An object is a specific instance of a class, created (instantiated) with its own attribute values.
- A constructor is a special method that runs when an object is created, setting up its initial attributes.
class Animal
private name
private sound
public procedure new(name, sound) // constructor
self.name = name
self.sound = sound
endprocedure
public function makeSound()
return self.sound
endfunction
endclass
dog = new Animal("Rex", "Woof") // an object (instance)
The three core principles
1. Encapsulation
Data and the methods that act on it are bundled together inside the object, and attributes are kept private. Access is only through public methods (getters/setters). This protects data from accidental/invalid changes — the object controls its own state.
2. Inheritance
A child (subclass) can inherit the attributes and methods of a parent (superclass), then add or change its own. This promotes code reuse and models "is-a" relationships (a Dog is an Animal). E.g. class Dog inherits Animal.
3. Polymorphism
Objects of different classes can be treated through a shared interface, and the same method name behaves differently depending on the object. Overriding lets a subclass replace an inherited method with its own version (e.g. each Animal subclass has its own makeSound()).
Benefits of OOP
- Modular and reusable (classes/inheritance).
- Easier to maintain and extend; data is protected (encapsulation).
- Good for large, collaborative projects (clear structure).
Worked example
Why make a class's attributes private and provide public methods to access them?
- This is encapsulation: it stops other code changing the data directly, so the object can validate changes and protect its state from invalid values. ✓
Common mistakes
- Confusing a class (blueprint) with an object (instance of it).
- Mixing up the three principles — encapsulation (bundling/hiding data), inheritance (reuse via parent/child), polymorphism (same method, different behaviour).
- Forgetting the constructor sets up a new object's initial state.
Exam tips
- Define class, object, attribute, method, constructor, instantiation precisely.
- Learn one-line definitions and a benefit of encapsulation, inheritance and polymorphism.
- Be ready to read/write simple class definitions and identify overriding.
Key facts to remember
- Class = blueprint; object = instance; created via a constructor.
- Encapsulation (bundle + hide data behind methods), inheritance (child reuses parent), polymorphism (same method name, different behaviour via overriding).
- OOP gives reusable, maintainable, modular code with protected data.