Inheritance, MRO & Abstract Classes
Extend classes with single and multiple inheritance, understand Python's Method Resolution Order (MRO), and enforce contracts with ABC.
Part 1: What You Will Learn
- Create subclasses that reuse and extend parent-class behaviour.
- Use
super()correctly when overriding methods. - Understand how Python chooses a method with the Method Resolution Order (MRO).
- Define abstract base classes (ABCs) that require subclasses to implement specific methods.
Part 2: Key Concepts
Inheritance models an is-a relationship. A subclass receives accessible attributes and methods from its base class, then can add or override behaviour. Abstract base classes define a common contract, while mixins provide small reusable behaviours that can be combined through multiple inheritance.
- Single inheritance: one subclass extends one base class.
- Multiple inheritance: a class inherits from more than one base class.
- MRO: the order Python follows when looking for a method or attribute.
- ABC: a base class that can declare required methods with
@abstractmethod.
Part 3: Topic-Specific Code Example
from abc import ABC, abstractmethod
from datetime import datetime
class NotificationService(ABC):
@abstractmethod
def send(self, message: str) -> None:
"""Send a notification."""
class TimestampMixin:
def timestamp(self) -> str:
return datetime.now().strftime("%H:%M:%S")
class ConsoleNotifier(NotificationService):
def send(self, message: str) -> None:
print(f"Console: {message}")
class UrgentConsoleNotifier(TimestampMixin, ConsoleNotifier):
def send(self, message: str) -> None:
print(f"[{self.timestamp()}] URGENT")
super().send(message.upper())
class EmailNotifier(NotificationService):
def __init__(self, address: str) -> None:
self.address = address
def send(self, message: str) -> None:
print(f"Email to {self.address}: {message}")
services: list[NotificationService] = [
UrgentConsoleNotifier(),
EmailNotifier("student@example.com"),
]
for service in services:
service.send("Assignment deadline tomorrow")
print("MRO:", [cls.__name__ for cls in UrgentConsoleNotifier.__mro__])Part 4: How the Example Works
The abstract NotificationService guarantees that every concrete notifier provides a send() method. UrgentConsoleNotifier combines a timestamp mixin with ConsoleNotifier. Its call to super().send() follows the class MRO, so Python continues to the next matching implementation instead of hard-coding a parent class name.
Part 5: Hands-On Practice
Mini project โ Payment Processor. Create an abstract PaymentMethod class with an abstract pay(amount) method. Implement CardPayment and EWalletPayment. Add a small AuditMixin that prints a timestamp before a payment is processed.
Part 6: Next Steps
Run and modify the examples in Visual Studio 2026, then continue to Lesson 11. Return to Python Tutorial Home to review the complete curriculum.