Abstract Classes in Python
Last Updated :
03 Sep, 2025
In Python, an abstract class is a class that cannot be instantiated on its own and is designed to be a blueprint for other classes. Abstract classes allow us to define methods that must be implemented by subclasses, ensuring a consistent interface while still allowing the subclasses to provide specific implementations.
When to Use Abstract Classes
Abstract classes are useful when you want to:
- Define a common interface for all subclasses (e.g., all animals must have a sound()).
- Enforce implementation of certain methods in child classes.
- Provide shared functionality (concrete methods) while still requiring subclasses to implement specific behavior.
Abstract Base Classes
An Abstract Base Class (ABC) defines methods that must be implemented by its subclasses, ensuring that the subclasses follow a consistent structure. ABCs allow you to define common interfaces that various subclasses can implement while enforcing a level of abstraction.
Python provides the abc module to define ABCs and enforce the implementation of abstract methods in subclasses.
Example: This example shows an abstract class Animal with an abstract method sound() and a concrete subclass Dog that implements it.
Python
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
return "Bark"
dog = Dog()
print(dog.sound())
Explanation:
- Animal is an abstract class (inherits from ABC) and defines an abstract method sound().
- Any class inheriting from Animal must implement sound().
- Dog provides its own implementation of sound(), so it can be instantiated.
- Running dog.sound() prints "Bark".
Abstract Methods
Abstract methods are methods that are defined in an abstract class but do not have an implementation. They serve as a blueprint for the subclasses, ensuring that they provide their own implementation.
Example: This example shows that trying to instantiate an abstract class directly raises an error.
Python
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass
# animal = Animal() # Raises: TypeError
Explanation: make_sound() is an abstract method in the Animal class, so it doesn't have any code inside it. If you try to instantiate Animal directly, Python will raise a TypeError since the method is not implemented.
Concrete Methods
Concrete methods are methods that have full implementations in an abstract class. These methods can be inherited by subclasses and used directly without needing to be redefined.
Example: This example defines a concrete method move() alongside an abstract method.
Python
class Dog(Animal):
def make_sound(self):
return "Bark"
dog = Dog()
print(dog.move()) # Uses concrete method from Animal
print(dog.make_sound())
Explanation: Here, Dog inherits the concrete method move() from Animal without redefining it, while also implementing its required abstract method make_sound().
Abstract Properties
Abstract properties work like abstract methods but are used for properties. These properties are declared with the @property decorator and marked as abstract using @abstractmethod.
Abstract properties enforce that a subclass provides the property’s implementation. They are especially useful when you want all subclasses to have a certain attribute (like species, category or type).
Example: This example defines species as an abstract property, which subclasses must implement.
Python
from abc import ABC, abstractmethod
class Animal(ABC):
@property
@abstractmethod
def species(self):
pass
class Dog(Animal):
@property
def species(self):
return "Canine"
# Instantiate the concrete subclass
dog = Dog()
print(dog.species)
Explanation:
- species is an abstract property in the Animal class and it is marked as @abstractmethod.
- The Dog class implements the species property, making it a concrete subclass that can be instantiated.
- Abstract properties enforce that a subclass provides the property’s implementation.
Abstract Class Instantiation
Abstract classes cannot be instantiated directly. This is because they contain one or more abstract methods or properties that lack implementations. Attempting to instantiate an abstract class results in a TypeError.
Example: This example shows the error raised when trying to instantiate an abstract class.
Python
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass
# animal = Animal()
# TypeError: Can't instantiate abstract class Animal with abstract methods make_sound
Explanation:
- The Animal class is abstract because it has the abstract method make_sound().
- Trying to create animal = Animal() directly raises a TypeError.
- Only concrete subclasses that implement all abstract methods (like Dog) can be instantiated.
Note: If a subclass does not implement all abstract methods or properties from its parent, it also remains abstract and cannot be instantiated.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice