← Back to Additional Topics

Class vs Interface / ABC

Additional TopicsMid

The Question

What is the difference between a class and an interface/ABC in Python?

What a Strong Answer Covers

  • `abc.ABC` + `@abstractmethod`. TypeError on instantiation. "Can have concrete methods.

Senior-Level Answer

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.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains ABCMeta/@abstractmethod mechanism, states you can't instantiate an ABC, gives a practical example of when to use each.

3/3 — Strong Answer

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).

Common Mistakes

  • Not knowing that abc.ABC is just syntactic sugar for ABCMeta
  • Saying you can instantiate an ABC — TypeError is raised
  • Forgetting that ABCs can include concrete methods, not just abstract ones
  • Missing typing.Protocol as the modern lightweight alternative for structural subtyping

Follow-Up Questions

  • What is typing.Protocol and how does it differ from ABC? — Protocol uses structural subtyping (duck typing) — any class with the required methods matches, without explicit inheritance. ABC requires explicit subclassing. Protocol is more Pythonic for library code.
  • Can an ABC have concrete (non-abstract) methods? — Yes — ABCs can mix abstract and concrete methods. Concrete methods provide default behavior that subclasses can override or inherit. This is similar to Java's abstract classes, not interfaces.
  • What does __subclasshook__ do? — It lets an ABC customize isinstance/issubclass checks. E.g., collections.abc.Iterable uses it to recognize any class with __iter__ as an Iterable without explicit registration.
  • When would you choose an ABC over a typing.Protocol? — Use ABC when you want explicit registration, want to provide default method implementations, or need to ensure the contract is enforced at instantiation. Use Protocol when you want structural typing without coupling to a class hierarchy.

Related Questions

  • CAP theorem
  • SQL vs NoSQL
  • Big O basics
  • N+1 problem
  • AI tools in workflow

Can You Explain This Cold?

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
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout