Skip to content

Abstract Base Class (ABC)

In one line

An ABC is a class with @abstractmethod decorators that cannot be instantiated until all abstract methods are implemented.

In simple words

An Abstract Base Class (ABC) defines a common interface that concrete subclasses must implement. You mark required methods with @abstractmethod. Python prevents instantiating the ABC or any subclass that hasn't implemented all abstract methods.

Use from abc import ABC, abstractmethod. ABCs are useful for:

  • Defining plugin interfaces
  • Ensuring consistent APIs across implementations
  • Documenting what methods a subclass must provide

ABCs can also provide default implementations for some methods, which subclasses can override.

Example

python
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self) -> float:
        pass
    
    @abstractmethod
    def perimeter(self) -> float:
        pass

class Circle(Shape):
    def __init__(self, r):
        self.r = r
    def area(self):
        return 3.14159 * self.r ** 2
    def perimeter(self):
        return 2 * 3.14159 * self.r

# shape = Shape()  # TypeError!
c = Circle(5)
print(f"Area: {c.area():.2f}")  # 78.54

Shape is abstract — cannot be instantiated. Circle must implement area() and perimeter() or it also can't be instantiated.

Common confusions

  • Confused with: interface

    The difference: Python ABCs can contain both abstract methods (must implement) and concrete methods (inherited as-is). Java interfaces (pre-8) could only have abstract methods. Python has no separate 'interface' keyword — ABCs serve that role.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary