Skip to content

abstract base class (ABC)

In Python, an abstract base class (ABC) is a class that can’t be instantiated on its own and is designed to be a blueprint for other classes, allowing you to define a common interface for a group of related classes.

ABCs allow you to define methods that must be created within any subclasses built from the abstract base class. This is useful for ensuring that derived classes implement particular methods from the base class, providing a consistent interface for different parts of your program.

To define an abstract base class, you inherit from abc.ABC and use the @abstractmethod decorator to mark methods that must be implemented by subclasses. Attempting to instantiate an abstract base class or a subclass that hasn’t implemented all abstract methods will raise a TypeError exception.

Examples

Here’s an example of how to define and use an abstract base class in Python:

Language: Python Filename: animals.py
from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def speak(self):
        pass

    def jump(self):
        return f"{self.__class__.__name__} is jumping"

Animal is an abstract base class with an abstract method .speak() and a regular method .jump(). Subclasses must implement all methods marked with @abstractmethod:

Language: Python
>>> from animals import Animal

>>> class Dog(Animal):
...     pass

>>> Dog()
Traceback (most recent call last):
    ...
TypeError: Can't instantiate abstract class Dog
⮑ without an implementation for abstract method 'speak'

Here’s a correct implementation:

Language: Python
>>> class Dog(Animal):
...     def speak(self):
...         return "Woof Woof"
...

>>> dog = Dog()
>>> dog.speak()
'Woof Woof'

>>> dog.jump()
'Dog is jumping'

Dog is a concrete class that inherits from Animal and implements the .speak() abstract method. It inherits the .jump() method from Animal.

Tutorial

Implementing Interfaces in Python: ABCs and Protocols

Learn how to implement interfaces in Python using abstract base classes, Protocols, and duck typing, and enforce method contracts cleanly.

advanced python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated March 10, 2026 • Reviewed by Dan Bader