What is the difference between a class and an interface/ABC in Python?
In Python, the distinction between a class and an Abstract Base Class (ABC) maps roughly to what other languages call 'class vs. interface,' though Python's model is more flexible due to its dynamic typing.
**A class** is a blueprint that bundles state (instance variables) and behavior (methods) with concrete implementations. You instantiate it directly with `ClassName()`. All methods have bodies. Python classes support multiple inheritance, so a class can inherit both state and behavior from multiple parents.
**An Abstract Base Class (ABC)** is a class that defines a *contract*—a set of method signatures that subclasses must implement—without providing all (or any) implementations. ABCs are created by inheriting from `abc.ABC` (or using `ABCMeta` as metaclass) and marking methods with `@abstractmethod`. You cannot instantiate an ABC directly; attempting to do so raises `TypeError`. Only a subclass that implements all abstract methods can be instantiated.
ABCs serve as formalized interfaces in Python's duck-typing world. They catch missing-implementation bugs at instantiation time rather than at first call. They also integrate with Python's `isinstance` and `issubclass` checks—a key feature for library authors who want type-checking without requiring concrete inheritance (via `__subclasshook__`).
Python's `collections.abc` module provides ABCs for standard protocols: `Iterable`, `Iterator`, `Sequence`, `Mapping`, `Callable`, etc. When you implement `__iter__` and `__next__`, your class is automatically recognized as an `Iterator` even without explicit inheritance.
The practical distinction for interviews: use a concrete class when you're providing full implementation. Use an ABC when you're defining a plugin/extension point or a protocol that multiple implementations must fulfill—e.g., a `StorageBackend` ABC with abstract `read()` and `write()` methods that `S3Backend` and `LocalBackend` both implement. This makes the contract explicit, aids documentation, and gives early error messages on incomplete implementations.
Compared to Java/C# interfaces: Python ABCs can include concrete (non-abstract) methods with default implementations, which makes them closer to Java's abstract classes than pure interfaces. Python 3.8+ structural subtyping (`typing.Protocol`) provides a lighter-weight alternative for duck-typing-compatible contracts without requiring explicit inheritance.
Explains ABCMeta/@abstractmethod mechanism, states you can't instantiate an ABC, gives a practical example of when to use each.
Covers ABCMeta vs. abc.ABC, explains isinstance integration and __subclasshook__, mentions collections.abc built-in ABCs, compares to typing.Protocol for structural subtyping, and gives a real design example (plugin pattern or storage backend).
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment