Abstraction & Abstract Classes
Define a common interface while allowing subclasses to hide different implementations behind that interface.
Depend on what an object does, not every internal detail
- 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.
Rapid Recall Deck
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”.
An abstract class establishes a contract
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.
Complete the common interface
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"Do not collapse two OOP principles into one
Abstraction vs encapsulation
A student says: “Abstraction and encapsulation both just mean making variables private.” Explain why this is incorrect.
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.
Teacher-created Paper 2 practice
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().
- Other modules can depend on a stable required interface without knowing every implementation detail, reducing coupling and supporting independent changes.
- Both concrete shapes must provide an
area()operation that callers can use consistently. class Square(Shape): def __init__(self, side): self.side = side def area(self): return self.side * self.side
Can you explain the benefit of abstraction?
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.
Finish the learning cycle
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.