Problem Statement
What are Abstract Base Classes (ABC) and when would you use them?
Explanation
An Abstract Base Class defines a formal interface by declaring abstract methods that subclasses must implement. It prevents instantiation until required methods are provided and communicates intent clearly to readers and tools.
Use ABCs when many implementations must share a contract, such as storage backends or payment gateways. They improve consistency, enable static checks, and reduce runtime surprises.
Code Solution
SolutionRead Only
from abc import ABC, abstractmethod
class Store(ABC):
@abstractmethod
def save(self, item): ...
class DB(Store):
def save(self, item): pass