Intermediate

Object-Oriented Programming

AicademyAicademy
·A-Level Computer Science·AQA 7517·6 min
4.1.3 Programming paradigms: OOP — classes, objects, encapsulation, inheritance, polymorphism, class diagrams

Procedural vs Object-Oriented Programming

Procedural programming structures code as a sequence of procedures that operate on data. Data and the code that manipulates it are kept separate.

Object-oriented programming (OOP) bundles data (attributes) and the procedures that operate on it (methods) together into objects. Programs are modelled as interactions between objects.

AspectProceduralObject-Oriented
OrganisationProcedures and data separateData and methods bundled in objects
ReuseVia procedure callsVia inheritance and composition
StateGlobal or passed as parametersEncapsulated inside objects
Best forSimple, sequential tasksComplex systems with many interacting entities

Classes and Objects

A class is a blueprint that defines the structure and behaviour of a category of objects.

An object is a specific instance of a class — it has its own copies of the attributes defined in the class.

CLASS Animal
    PRIVATE name : STRING
    PRIVATE sound : STRING

    PUBLIC PROCEDURE new(aName, aSound)
        name ← aName
        sound ← aSound
    ENDPROCEDURE

    PUBLIC FUNCTION speak()
        RETURN name + " says " + sound
    ENDFUNCTION
ENDCLASS

dog ← NEW Animal("Dog", "Woof")
cat ← NEW Animal("Cat", "Meow")
OUTPUT dog.speak()    // Dog says Woof
OUTPUT cat.speak()    // Cat says Meow

dog and cat are two distinct instances of the Animal class. Each has its own name and sound attribute values.

Encapsulation

Encapsulation is the bundling of data (attributes) and the methods that operate on it inside a class, with controlled external access.

  • Private attributes/methods: accessible only from within the class
  • Public attributes/methods: accessible from outside the class

Encapsulation protects data from uncontrolled modification. External code accesses private data only through getter (accessor) and setter (mutator) methods:

CLASS BankAccount
    PRIVATE balance : REAL

    PUBLIC PROCEDURE new(initialBalance)
        balance ← initialBalance
    ENDPROCEDURE

    PUBLIC FUNCTION getBalance()
        RETURN balance
    ENDFUNCTION

    PUBLIC PROCEDURE deposit(amount)
        IF amount > 0 THEN
            balance ← balance + amount
        ENDIF
    ENDPROCEDURE
ENDCLASS

Encapsulation prevents external code from setting balance to a negative value directly. The deposit method enforces the business rule that deposits must be positive.

Inheritance

Inheritance allows a class (the subclass or child class) to inherit the attributes and methods of another class (the superclass or parent class), and add or override them.

CLASS Shape
    PROTECTED colour : STRING

    PUBLIC FUNCTION getColour()
        RETURN colour
    ENDFUNCTION

    PUBLIC FUNCTION area()
        RETURN 0        // Default — overridden in subclasses
    ENDFUNCTION
ENDCLASS

CLASS Rectangle INHERITS Shape
    PRIVATE width : REAL
    PRIVATE height : REAL

    PUBLIC PROCEDURE new(w, h, c)
        width ← w
        height ← h
        colour ← c
    ENDPROCEDURE

    PUBLIC FUNCTION area()
        RETURN width * height    // Overrides Shape.area()
    ENDFUNCTION
ENDCLASS

r ← NEW Rectangle(5, 3, "red")
OUTPUT r.area()         // 15
OUTPUT r.getColour()    // red (inherited from Shape)

The Rectangle class inherits getColour() from Shape and overrides area() with its own implementation.

Want more lessons like this one?

Generate lessons on anything you study. Free account, no card needed.

Start generating

Polymorphism and Class Diagrams

Polymorphism means "many forms" — a method with the same name can behave differently depending on the object it is called on.

CLASS Circle INHERITS Shape
    PRIVATE radius : REAL
    ...
    PUBLIC FUNCTION area()
        RETURN 3.14159 * radius * radius  // Different from Rectangle.area()
    ENDFUNCTION
ENDCLASS

Both Rectangle and Circle have an area() method, but each computes it differently. Code that holds a list of Shape objects can call area() on each without knowing whether it is a Rectangle or Circle.

Class diagram notation shows the structure of classes and their relationships:

          ┌──────────────┐
          │    Shape     │
          │──────────────│
          │ - colour     │
          │──────────────│
          │ + getColour()│
          │ + area()     │
          └──────┬───────┘
                 │ inherits
        ┌────────┴─────────┐
        ▼                  ▼
┌───────────────┐  ┌───────────────┐
│   Rectangle   │  │    Circle     │
│───────────────│  │───────────────│
│ - width       │  │ - radius      │
│ - height      │  │───────────────│
│───────────────│  │ + area()      │
│ + area()      │  └───────────────┘
└───────────────┘
  • - prefix: private attribute/method
  • + prefix: public attribute/method
  • Arrow from subclass to superclass: inheritance relationship

Advantages of OOP

OOP is widely used for complex software systems because:

AdvantageExplanation
EncapsulationHides internal implementation; reduces unintended interference between components
InheritanceReuse code across related classes; extend existing classes without modifying them
PolymorphismWrite generic code that works with any subclass; easier to add new types
ModularityEach class is an independent unit; easier to develop, test, and maintain
ReusabilityClasses can be imported into new projects as libraries

Common Exam Mistakes

1. Confusing a class and an object

A class is the blueprint; an object is an instance created from the blueprint. You can have many objects from one class, each with different attribute values.

2. Forgetting NEW when creating an object

dog ← Animal("Dog") is wrong in AQA pseudocode. Instantiation requires NEW: dog ← NEW Animal("Dog").

3. Treating PROTECTED and PRIVATE as identical

PRIVATE attributes are accessible only within the declaring class. PROTECTED attributes are accessible within the declaring class and any subclass. A PRIVATE attribute in a superclass is not directly accessible in a subclass — the subclass must use getters.

4. Describing polymorphism incorrectly

Polymorphism is not simply "different classes having methods with the same name." It is that the same method call on objects of different types (sharing a superclass) executes different code depending on the actual type of the object at runtime.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set — personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Subroutines and Recursion

Next

Arrays and Abstract Data Types

Related lessons

8 Slides

Lesson

Programming Fundamentals

A-Level Computer Science · AQA 7517

10 hours ago

8 Slides

Lesson

Subroutines and Recursion

A-Level Computer Science · AQA 7517

10 hours ago