🔒 Pro · Component 1 · 1.2.4 Types of Programming Language
1.2.4e Object-Oriented Programming (OOP)
OCR H446 · A Level Computer Science · ~14 min read
Notes
Video
Slides
Worksheet
Quiz

Introduction to Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that structures programs as collections of objects. Each object combines attributes (data) and methods (behaviours) into a single unit. OOP models real-world entities directly in code.

Classes and Objects

A class is a blueprint or template that defines the attributes and methods that objects of that type will have. It does not represent a specific entity — it describes the structure.

An object is a specific instance of a class, created from the class blueprint. Each object has its own values for the class's attributes.

# Class definition
class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def makeSound(self):
        print("...")

# Creating objects (instances)
cat = Animal("Luna", "Cat")
dog = Animal("Rex", "Dog")

Here, Animal is the class (blueprint); cat and dog are two distinct objects (instances), each with their own name and species attribute values.

The Four Pillars of OOP

1. Encapsulation

Encapsulation means bundling an object's data (attributes) and the methods that operate on that data together inside a class, and controlling access to the internal data from outside.

  • Private attributes are hidden from outside the class (prefixed with __ in Python, or declared private in Java/C++).
  • Public methods (getters/setters) control how external code accesses or modifies private data.
  • This protects the object's internal state from unintended modification — you must go through the class's own methods to change data.
  • Benefit: reduces coupling between different parts of a program; the internal implementation of a class can change without breaking external code that uses it.
class BankAccount:
    def __init__(self, balance):
        self.__balance = balance # private attribute

    def getBalance(self): # getter
        return self.__balance

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

External code cannot directly access __balance — it must call getBalance() or deposit(). This protects the balance from invalid direct assignment like account.__balance = -999.

2. Inheritance

Inheritance allows a subclass (child class) to inherit attributes and methods from a superclass (parent class). The subclass extends the parent, adding or overriding behaviour.

  • Promotes code reuse — common functionality is defined once in the parent class.
  • Creates an "is-a" relationship: a Dog is-an Animal; a SavingsAccount is-a BankAccount.
  • Subclasses can override parent methods to provide specific behaviour.
  • Python uses: class Dog(Animal):
class Dog(Animal): # Dog inherits from Animal
    def __init__(self, name):
        super().__init__(name, "Dog") # call parent constructor
        self.tricks = []

    def makeSound(self): # overrides parent method
        print("Woof!")

    def learn(self, trick):
        self.tricks.append(trick)

3. Polymorphism

Polymorphism means "many forms" — objects of different classes can be treated through the same interface, but each responds differently to the same method call. It relies on method overriding from inheritance.

  • Allows code to be written that works with objects of any compatible type, without needing to know the exact class.
  • Example: makeSound() is defined on Animal, but Dog, Cat, and Bird each override it with their own implementation.
class Cat(Animal):
    def makeSound(self):
        print("Meow!")

animals = [Dog("Rex"), Cat("Luna"), Dog("Buddy")]
for animal in animals:
    animal.makeSound() # Woof! Meow! Woof! — different behaviour, same call

The loop does not need to check the type of each object — it calls makeSound() on each, and polymorphism ensures the correct version runs.

4. Abstraction

Abstraction means hiding complex implementation details and exposing only what is necessary. The user of a class only sees the public interface — the methods and their signatures — not how they are implemented internally.

  • Example: when you call account.deposit(100), you don't need to know how the balance is stored internally — you just know that calling deposit adds to the balance.
  • Related concept: abstract classes — classes that define method signatures but provide no implementation; subclasses must implement them.
  • Abstraction reduces complexity and allows the programmer to focus on what an object does, not how it does it.

Constructors

A constructor is a special method that runs automatically when an object is created. It initialises the object's attributes.

  • In Python: __init__(self, ...)
  • In Java/C#/C++: a method with the same name as the class.
  • The self parameter in Python refers to the specific instance being created.

Class Diagrams (UML)

In the exam you may be given or asked to draw a class diagram. The standard format (UML) shows:

SectionContainsExample
Class nameName of the classBankAccount
AttributesData fields with types and visibility (- private, + public)- balance : Real
- owner : String
MethodsProcedure/function names, parameters, return types+ deposit(amount : Real) : void
+ getBalance() : Real

Association, Aggregation and Composition

Classes can relate to each other in different ways:

  • Association: one class uses another (e.g. a Student object attends a Course).
  • Aggregation: a "has-a" relationship where the parts can exist independently (e.g. a Library has Books, but Books exist without the Library).
  • Composition: a strong "has-a" where parts cannot exist without the whole (e.g. a House has Rooms — Rooms don't exist without the House).

Summary Table

PillarMeaningBenefit
EncapsulationBundle data + methods; hide private dataData protection; reduced coupling
InheritanceSubclass inherits from superclassCode reuse; models is-a relationships
PolymorphismSame interface, different behaviourFlexible, extensible code
AbstractionHide complexity; expose only interfaceSimplifies use of complex systems
Exam tip: Learn all four OOP pillars with definitions and examples. Encapsulation, inheritance, polymorphism, and abstraction can each be worth 2–4 marks. Be able to identify them in given code snippets.
Exam tip: Know the difference between a class and an object: a class is the blueprint/template (defined once); objects are specific instances created from it (many can exist). Also know what a constructor does.
⚠ Common Mistakes
  • Confusing encapsulation and abstraction — encapsulation is about HIDING DATA (making attributes private and using getters/setters). Abstraction is about HIDING COMPLEXITY (the user doesn't need to know how something is implemented, just how to use it). They overlap but are distinct concepts.
  • Saying 'inheritance' when describing polymorphism — they are related but different. Inheritance is when a class inherits attributes/methods. Polymorphism is when the same method call produces different behaviour depending on which subclass the object belongs to.
  • Forgetting to say 'self' is the instance — in Python, 'self' is a reference to the specific object calling the method, not to the class. Each object has its own copy of attribute values.
✓ Notes completed!
Video coming soon
Click to advance · Arrow keys also work
Click slide or press arrow keys to navigate

Worksheet — 1.2.4e Object-Oriented Programming

8 questions · 20 marks · instantly marked

Q1Define the terms 'class' and 'object' in OOP, and state how they differ.[2 marks]
✓ Mark scheme
A class is a blueprint/template that defines the attributes and methods that objects of that type will have [1]; an object is a specific instance created from a class, with its own values for the class's attributes [1]. One class can be used to create many distinct objects.
Q2Explain what 'encapsulation' means in OOP and describe TWO benefits it provides.[4 marks]
✓ Mark scheme
Encapsulation means bundling an object's attributes and the methods that operate on them together inside a class, and restricting direct access to internal data (making attributes private) [1]; benefit 1: data protection — private attributes cannot be modified directly from outside the class; all modifications must go through the class's own methods which can validate the data [1]; benefit 2: reduced coupling — the internal implementation of a class can change without affecting external code that uses it, as long as the public interface (method signatures) remains the same [1]; benefit 3 (any): modularity — each class is a self-contained unit; easier to maintain and debug [1].
Q3Explain the OOP concept of inheritance, including the terms 'superclass' and 'subclass'. State one advantage of using inheritance.[3 marks]
✓ Mark scheme
Inheritance allows a subclass (child class) to inherit attributes and methods from a superclass (parent class) [1]; the superclass contains common attributes and methods shared by all subclasses; the subclass can add new attributes/methods or override inherited ones [1]; advantage: code reuse — common functionality is defined once in the superclass rather than duplicated in each subclass, making code easier to maintain [1].
Q4What is polymorphism in OOP? Give a concrete example showing how it works in practice.[3 marks]
✓ Mark scheme
Polymorphism means objects of different classes can be treated through the same interface — the same method call produces different behaviour depending on the actual class of the object [1]; example: a list contains objects of type Dog, Cat, and Bird — all inherit from Animal and each override the makeSound() method. Calling makeSound() on each object in a loop produces 'Woof', 'Meow', 'Tweet' respectively [1]; this works because the method call is resolved at runtime based on the actual object type, not the reference type — the code doesn't need separate if/else checks for each type [1].
Q5Distinguish between encapsulation and abstraction. Why might students confuse these two concepts?[3 marks]
✓ Mark scheme
Encapsulation focuses on HIDING DATA — making attributes private and controlling access through public methods; it is about protecting the internal state of an object [1]; abstraction focuses on HIDING COMPLEXITY — showing only what a user needs to know (the public interface/method names and what they do) while hiding how they work internally; it is about simplifying use [1]; students confuse them because both involve 'hiding' — encapsulation hides the data, abstraction hides the implementation logic. They often occur together in a well-designed class [1].
Q6A student designs a class Vehicle with attributes make and speed, and methods accelerate() and brake(). A subclass Car inherits from Vehicle and adds a numDoors attribute. Draw a UML class diagram for both classes, showing the inheritance relationship.[3 marks]
✓ Mark scheme
Vehicle class: class name 'Vehicle'; attributes: - make : String, - speed : Integer; methods: + accelerate() : void, + brake() : void [1]; Car class: class name 'Car'; additional attribute: - numDoors : Integer; inherits all attributes/methods from Vehicle [1]; inheritance relationship shown as an arrow from Car pointing to Vehicle (arrow points UP to the superclass); text description or diagram showing Car is-a Vehicle [1].
Q7What is a constructor? State what it does, when it is called, and give an example of a constructor in Python for a class Student with attributes name and grade.[2 marks]
✓ Mark scheme
A constructor is a special method that runs automatically when an object is instantiated (created) from a class; it initialises the object's attributes with starting values [1]; Python example: def __init__(self, name, grade): self.name = name; self.grade = grade — called automatically when you write s = Student("Alice", "A") [1].
Q8Explain the difference between aggregation and composition in OOP. Give an example of each.[2 marks]
✓ Mark scheme
Aggregation: a 'has-a' relationship where the parts can exist independently of the whole; example: a Library has Books — if the Library is destroyed, the Books still exist [1]; composition: a strong 'has-a' relationship where the parts cannot exist independently — they are owned by and live and die with the whole; example: a House has Rooms — Rooms do not exist without the House; or an Order has OrderLines [1].
Topic Quiz
1 of 15
You scored
out of 15
🎯

Mini Test — 1.2.4e OOP

  • 10 questions · 10 marks · 10 minutes
  • 5 MCQ + 5 short answer
Card 1 of 15
Click to reveal
🎉
Complete!
TermDefinition
← 1.2.4d Programming Paradigms 1.2.4 Types of Programming Language Next: 1.3.1a Compression →
🔒
Pro Content
Subscribe to access all 69 OCR H446 A Level lessons.
£7.99/month
or £59/year
Subscribe now →