# Object-oriented programming concepts

> CIE A-Level Computer Science · 9618 (2022-2024)
> Source: https://www.owlsprep.com/study/cie-9618-u11-object-oriented-programming-concepts/

This module covers core object-oriented programming (OOP) concepts tested in CIE 9618. You will learn the relationship between classes and objects, the four core OOP principles, and how OOP models complex systems.

**Prerequisites:** [Basic procedural programming concepts](https://www.owlsprep.com/study/cie-9618-u10-procedural-programming-concepts/)

## Learning objectives

- Distinguish between classes and objects in object-oriented programming
- Explain the four core OOP principles: encapsulation, inheritance, polymorphism, abstraction
- Apply OOP concepts to model simple real-world scenarios
- Correctly use OOP terminology to answer CIE 9618 exam questions

## Classes and Objects

**Class** — A reusable blueprint or template that defines the common attributes (data) and methods (behaviours) that all objects of that type will share.

*Example:* A `Car` class defines that all cars have a colour, model, and can accelerate.

An object is an instance of a class. Each object has its own unique values for the attributes defined by the class, but shares the same methods as all other objects of that class. This allows multiple distinct entities to be created from a single template.

**Worked example:** Model a simple bank account system using a class and two object instances. List the attributes and methods required.

1. First, define the `BankAccount` class as the blueprint, with common attributes and methods:
2. Attributes: account holder name, current balance; Methods: deposit funds, withdraw funds, check balance
3. Next, create two distinct object instances from the `BankAccount` class, with unique attribute values:
4. 1. `aliceAccount = BankAccount("Alice Smith", 1500)` 2. `bobAccount = BankAccount("Bob Jones", 3250)`
5. Both objects use the same `deposit()` and `withdraw()` methods defined in the class, but operate on their own independent balance values.

## Encapsulation and Abstraction

**Encapsulation** — The principle of bundling attributes and methods within a class, and restricting direct access to internal data to prevent unintended modification.

*Example:* A bank account's balance cannot be changed directly, only via validated deposit/withdraw methods.

Abstraction is closely related to encapsulation: it means hiding unnecessary implementation details from the user, only exposing the essential features of an object. This reduces complexity and allows users to interact with objects without understanding how they work internally.

> **tip**
>
> In CIE exams, you will often be asked to distinguish between these two: encapsulation is about data bundling and access restriction, abstraction is about hiding implementation complexity.

**Worked example:** Explain how encapsulation works in a `Student` class that stores student exam grades.

1. The `Student` class bundles the `grades` attribute (a list of marks) and methods to add a grade and calculate the average grade.
2. The `grades` attribute is marked as private, meaning it cannot be accessed or modified directly from outside the class.
3. All changes to grades must go through the `addGrade()` method, which validates that the new grade is between 0 and 100 before adding it, preventing invalid data.
4. Users only interact with `addGrade()` and `getAverage()`, so the internal implementation of how grades are stored is abstracted away.

## Inheritance

**Inheritance** — A mechanism where a new (child/sub) class derives attributes and methods from an existing (parent/super) class, enabling code reuse and hierarchical classification.

Inheritance allows child classes to override inherited methods to provide specialised behaviour, while still retaining all features of the parent class. This supports hierarchical modelling of related concepts, avoiding redundant code.

**Worked example:** Use inheritance to model different types of vehicles for a municipal parking system.

1. First create a parent superclass `Vehicle` that defines common attributes and methods all vehicles share:
2. Attributes: registration number, manufacturer, model; Method: `getRegistrationNumber()`
3. Create two child subclasses `Car` and `Motorcycle` that inherit all members from `Vehicle`:
4. `Car` adds `numberOfDoors` attribute and `isElectric()` method; `Motorcycle` adds `engineCapacity` attribute and `hasSideCar()` method
5. Both child classes automatically inherit the `registrationNumber` attribute and `getRegistrationNumber()` method, so this code does not need to be rewritten for each subclass.

## Polymorphism

**Polymorphism** — The ability of objects from different subclasses to respond to the same method name with different, class-specific behaviour.

The most common form of polymorphism in OOP is method overriding, where a child class provides a specific implementation of a method that is already defined in the parent class. This allows code to interact with any parent class object without needing to know which specific subclass it belongs to.

**Worked example:** Demonstrate polymorphism using the vehicle parking example, where all vehicles need to calculate their parking fee.

1. Define a method `calculateParkingFee(hours)` in the parent `Vehicle` class with a default rate of \$2 per hour.
2. Override the method in the `Car` subclass: cars have a minimum fee of \$5, so the method returns `max(5, 2 * hours)`.
3. Override the method in the `Motorcycle` subclass: motorcycles have a flat rate of \$1.50 per hour, so the method returns `1.5 * hours`.
4. When looping through a list of `Vehicle` objects to calculate total fees, the correct version of `calculateParkingFee()` is called automatically for each object, regardless of its subclass.

> **Exam tip:** When asked to explain polymorphism in exams, always include an example of method overriding to earn full marks.

## Common pitfalls

- **Wrong:** Confusing classes and objects, calling a class an instance of an object
  - Why it fails: The relationship is reversed: classes are templates, objects are instances created from the template
  - Correct: Always state that an object is an instance of a class, which acts as the template for the object
- **Wrong:** Mixing up encapsulation and abstraction, defining encapsulation as hiding implementation details
  - Why it fails: The core focus of each concept is different, and examiners mark down for mixing the definitions
  - Correct: Remember: encapsulation = data bundling + access restriction; abstraction = hiding unnecessary implementation complexity
- **Wrong:** Stating that inheritance flows from child class to parent class
  - Why it fails: Inheritance creates new classes from existing classes, so the direction is reversed in this claim
  - Correct: Child (sub) classes inherit attributes and methods from existing parent (super) classes
- **Wrong:** Only defining polymorphism as 'many shapes' with no practical example
  - Why it fails: Examiners expect an explanation of how polymorphism works in OOP, not just a translation of the term
  - Correct: Explain polymorphism as the ability of different subclass objects to respond to the same method name with different behaviour, usually via method overriding

## Cheatsheet

| OOP Concept | Core Definition | Main Purpose |
| --- | --- | --- |
| Class | Blueprint/template for objects | Define shared attributes and methods |
| Object | Instance of a class | Represent individual entities |
| Encapsulation | Bundle data + restrict access | Prevent invalid modification of internal data |
| Abstraction | Hide implementation details | Reduce complexity for end users |
| Inheritance | Child inherits from parent | Enable code reuse and hierarchical modelling |
| Polymorphism | Same method name, different behaviour | Allow generic code for multiple subclasses |

## What's next

Now that you understand the core concepts of OOP, you can move on to implementing these concepts in code, which is tested in both Paper 1 and Paper 2 of CIE 9618. OOP is the foundation for most modern software development, so mastering these core concepts will help you model complex problems efficiently, answer theory questions correctly, and write structured, maintainable code for your programming projects. Building a solid foundation here also prepares you for further computer science study at university.

- [Declarative programming concepts](https://www.owlsprep.com/study/cie-9618-u11-declarative-programming-concepts/)
- [Software development](https://www.owlsprep.com/study/cie-9618-u12-overview/)
- [Software lifecycle](https://www.owlsprep.com/study/cie-9618-u12-software-lifecycle/)

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/cie-9618-u11-object-oriented-programming-concepts/
