Abstract Classes in Python Explained
Posted in Python on January 25, 2023 by Jessica Rose ‐ 4 min read

An abstract method is a method that has declarations of variables, and function definitions but no implementations.
A Python class with these abstract methods is called abstract methods.
An abstract method in Python is a method that is declared in an abstract base class (ABC) but does not have an implementation.
The main purpose of an abstract method is to define a contract for subclasses to implement, without specifying how the implementation should be done.
When a class contains an abstract method, it is considered an abstract class and cannot be instantiated.
For example, consider the following abstract base class:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
In this example, the Shape class has an abstract method called “area” that does not have an implementation.
Any class that inherits from Shape must provide an implementation for the “area” method to be instantiated.
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * (self.radius ** 2)
c = Circle(5)
print(c.area()) # 78.5
Now, let’s consider an example of a rectangle.
class Rectangle(Shape):
def __init__(self, length,breath):
self.length = length
self.breath = breath
def area(self):
return self.length * self.breath
r = Rectangle(10,15)
print(r.area()) # 150
In the above example, the Circle class and Rectangle provide an implementation for the “area” method by defining how to calculate the area of a circle, and it can be instantiated now.
Why Abstract Classes?
An abstract class serves as a template for creating subclasses. It defines the basic structure and behavior that all child classes should adhere to. Essentially, an abstract class acts as the foundation for creating related child classes.
For example, the abstract class Shape may include an abstract method called “area()”, which defines the contract for calculating the area of a shape. The subclasses Circle and Rectangle would then provide their implementation of this method based on their unique geometric properties.
In this way, abstract classes provide a way to ensure consistency and adherence to specific rules among related child classes. It allows for a more organized and maintainable codebase.
How to Write abstract Classes in Python
To create an abstract class in Python, you can use the built-in abc (Abstract Base Class) module. The process involves a few steps:
Import the ABC class and abstract method decorator from the abc module.
Declare your abstract class as a subclass of the ABC class. This informs the Python interpreter that the class is intended to be an abstract class and cannot be instantiated.
Use the @abstractmethod decorator on any methods that should be considered abstract and have no implementation in the abstract class.
When should you implement Abstract classes?
An abstract class in Python is useful in the following situations:
Contract for subclasses
: An abstract class can be used to define a set of methods that all subclasses must implement. This ensures that all subclasses have certain functionality and promotes consistency in the codebase.Common interface
: An abstract class can define a common interface that all subclasses must implement. This allows objects of different classes to be treated similarly, making the code more modular and reusable.Default implementation
: An abstract class can provide a default implementation for some of its methods, which can be overridden by subclasses if needed. This will save you time and effort developers, as they donβt have to implement the same functionality multiple times.Base class
: An abstract class can act as a base class for other classes, providing a common set of functionality and structure. This can make it easier to manage and maintain the codebase, as well as make it easier to add new functionality to the codebase.
An abstract class is useful when you want to define a blueprint for other classes, ensuring that they provide certain functionality and promoting consistency in the codebase.
It helps to separate the interface and implementation of a class, and also helps to promote inheritance, polymorphism and abstraction in the codebase.
Conclusion
In summary, abstract methods are a way to define a contract for subclasses to implement, while allowing the base class to remain abstract and not be instantiated.
It helps to ensure that subclasses provide certain functionality, and also helps to promote consistency and maintainability in the codebase.