Learning

Abstraction & Interfaces

Abstraction & Interfaces

Abstract Classes

python
from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass @abstractmethod def perimeter(self): pass # shape = Shape() # TypeError: Can't instantiate abstract class

Concrete Implementations

python
class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14159 * self.radius ** 2 def perimeter(self): return 2 * 3.14159 * self.radius class Rectangle(Shape): def __init__(self, w, h): self.w, self.h = w, h def area(self): return self.w * self.h def perimeter(self): return 2 * (self.w + self.h) c = Circle(5) print(c.area()) # 78.53975

Partial Implementation in Abstract Classes

python
class DataProcessor(ABC): @abstractmethod def fetch_data(self): pass @abstractmethod def parse_data(self, raw): pass # Non-abstract method — provides shared logic! def process(self): raw = self.fetch_data() parsed = self.parse_data(raw) return f'Processed: {parsed}' class ApiProcessor(DataProcessor): def fetch_data(self): return '{"value": 42}' def parse_data(self, raw): import json return json.loads(raw)['value'] print(ApiProcessor().process()) # 'Processed: 42'

Abstract Properties

python
class Config(ABC): @property @abstractmethod def max_connections(self): pass class DevConfig(Config): @property def max_connections(self): return 5 class ProdConfig(Config): @property def max_connections(self): return 100 # Missing @property would fail at instantiation!
Key Rules
  • •Abstract classes CANNOT be instantiated — they exist only as blueprints for subclasses
  • •Subclasses MUST implement ALL abstract methods, or they too remain abstract and can't be instantiated
  • •Abstract classes CAN have concrete (non-abstract) methods that provide shared logic to subclasses
  • •Use @abstractproperty (or @property + @abstractmethod) for mandatory read-only attributes
  • •ABCs catch missing implementations at IMPORT TIME, not at method call time — fail fast
Your Task

Create an abstract class `Notification` with abstract methods `send(self, message)` and `get_channel(self) -> str`, plus a concrete method `notify(self, message)` that prints `f'Sending via {self.get_channel()}: {message}'` then calls `self.send(message)`. Create `EmailNotification` and `SMSNotification` implementing both abstract methods. Demonstrate calling `notify()` on both.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should import ABC and abstractmethod
Notification should inherit from ABC
Should have two @abstractmethod decorated methods
notify() should call self.get_channel()
notify() should call self.send(message)
Should create both Email and SMS notification