IB Computer Science HL · Year 2 · Lesson 18

Abstraction & Abstract Classes

Define a common interface while allowing subclasses to hide different implementations behind that interface.

Paper 250 minutesB3.2.3 · HL only
Targets

Depend on what an object does, not every internal detail

2 / 9
  • explain abstraction as an OOP design principle;
  • explain how abstraction supports modular code fragments;
  • explain how abstract classes establish a common interface for subclasses;
  • read and complete a small Python abstract-class example.
B3.2.3
Learn + retrieve

Rapid Recall Deck

3 / 9

Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.

B3.2.3 · Explain

An abstract class establishes a contract

4 / 9
from abc import ABC, abstractmethod

class Report(ABC):
    @abstractmethod
    def build(self):
        pass

class SalesReport(Report):
    def build(self):
        return "sales report ready"

class AttendanceReport(Report):
    def build(self):
        return "attendance report ready"

Read the interface

What requirement does Report establish for its concrete subclasses, and what detail does it leave to those subclasses?

Every concrete subclass must provide a build() operation. The abstract class defines that common interface but leaves the exact implementation/output to each subclass.

Apply

Complete the common interface

5 / 9

Why not instantiate the parent?

Explain why Report() should not represent a finished report object in this design.

Report defines incomplete/common behaviour rather than a concrete implementation. It exists to establish what subclasses must provide.

Complete a subclass

Construct a subclass FinanceReport that implements build() and returns "finance report ready".

class FinanceReport(Report):
    def build(self):
        return "finance report ready"
Concept distinction

Do not collapse two OOP principles into one

6 / 9

Abstraction vs encapsulation

A student says: “Abstraction and encapsulation both just mean making variables private.” Explain why this is incorrect.

Abstraction: what essential interface should outside code rely on?
Encapsulation: how is state/implementation bundled and controlled?

Abstraction presents essential operations while hiding unnecessary implementation details; an abstract class can define a common interface. Encapsulation groups state and behaviour and controls access to internal state. Private attributes are one encapsulation technique, not the definition of abstraction.

Exam lens

Teacher-created Paper 2 practice

7 / 9

Paper 2 mini-set

1. Explain how an abstract class can support modular code.

2. A system has Shape with an abstract area() method and subclasses Rectangle and Circle. Explain what the common interface guarantees.

3. Assume Shape is already defined as an abstract class requiring an area() method. Construct the smallest valid Python subclass Square that stores side and implements area().

  1. Other modules can depend on a stable required interface without knowing every implementation detail, reducing coupling and supporting independent changes.
  2. Both concrete shapes must provide an area() operation that callers can use consistently.
  3. class Square(Shape):
        def __init__(self, side):
            self.side = side
    
        def area(self):
            return self.side * self.side
Exit check

Can you explain the benefit of abstraction?

8 / 9

Exit explanation

Why is a common interface useful even when subclasses use completely different algorithms internally?

Calling code can use the same operation without depending on each implementation, so subclasses can vary internally while remaining interchangeable through the interface.

Homework

Finish the learning cycle

9 / 9

IA — main task

Continue Criterion D development/testing. Identify one interface or module boundary in your product and check whether other code depends on unnecessary internal details.

Paper 2 — short practice

Recreate the abstract Report example and explain abstraction versus encapsulation in two clear sentences.