Corsi correlati
Guarda tutti i corsiHow to Create an Abstract Class in Python
A Beginner-Friendly Guide to Python Abstract Base Classes (ABC)

An abstract class is a class that cannot be instantiated directly. Instead, it serves as a template for other classes, defining methods that must be implemented by its subclasses.
Abstract classes are useful when you want to enforce a common interface across multiple classes while allowing each subclass to provide its own implementation. In Python, abstract classes are implemented using the abc module (Abstract Base Classes).
Why Use Abstract Classes
Abstract classes help organize code and enforce consistent structure across related classes. They are commonly used when:
- multiple classes share the same conceptual behavior;
- certain methods must be implemented by every subclass;
- you want to define a clear blueprint for derived classes.
For example, if you create different types of vehicles, each vehicle might need its own move() method, but the behavior will differ between a car, a boat, and an airplane.
Run Code from Your Browser - No Installation Required

Importing the Required Module
Before creating an abstract class in Python, you need to import the necessary tools from the built-in abc module. The name abc stands for Abstract Base Classes and provides functionality for defining abstract classes and abstract methods.
from abc import ABC, abstractmethod
The ABC class is used as the base class for defining abstract classes. The abstractmethod decorator marks methods that must be implemented by subclasses. Once a method is marked as abstract, any subclass must provide its own implementation before it can be instantiated.
Creating an Abstract Class
In the example below, the Animal class acts as a blueprint for other classes. It defines a method called speak(), but it does not implement the behavior itself. Any subclass derived from Animal must provide its own implementation of this method.
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def speak(self):
pass
Here the class Animal inherits from ABC, which tells Python that this class may contain abstract methods. The @abstractmethod decorator marks the speak() method as a method that must be implemented by subclasses. Because the method is abstract, its body only contains pass, meaning the base class does not provide the actual behavior.
As a result, the Animal class itself cannot be used to create objects. Python prevents instantiation because the class contains an abstract method that has not yet been implemented. Attempting to create an instance will raise a TypeError.
animal = Animal() # TypeError
This restriction ensures that abstract classes remain templates rather than usable objects. Their purpose is to define a structure that other classes must follow. Once a subclass implements the required methods, that subclass can be instantiated normally and used in the program.
Start Learning Coding today and boost your Career Potential

Implementing the Abstract Class
Once an abstract class defines the required methods, subclasses must provide their own implementation for those methods. This is how abstract classes enforce structure while still allowing different behavior in each subclass.
In the example below, the classes Dog and Cat inherit from the Animal abstract class and implement the required speak() method.
class Dog(Animal):
def speak(self):
return "Bark"
class Cat(Animal):
def speak(self):
return "Meow"
Because both classes implement the speak() method, they satisfy the requirements of the abstract class. This means Python now allows objects of these classes to be created and used in the program.
dog = Dog()
cat = Cat()
print(dog.speak())
print(cat.speak())
Each subclass provides its own behavior while still following the structure defined by the abstract class. This makes abstract classes useful for designing systems where multiple classes share the same interface but implement it in different ways.
FAQs
Q: Can you instantiate an abstract class in Python?
A: No. Abstract classes cannot be instantiated directly. They must be subclassed first.
Q: What module is used to create abstract classes in Python?
A: The built-in abc module.
Q: What happens if a subclass does not implement an abstract method?
A: Python will raise a TypeError when you try to create an instance of that subclass.
Q: Can abstract classes contain normal methods?
A: Yes. Abstract classes can include both abstract methods and fully implemented methods.
Corsi correlati
Guarda tutti i corsiThe 80 Top Java Interview Questions and Answers
Key Points to Consider When Preparing for an Interview
by Daniil Lypenets
Full Stack Developer
Apr, 2024・30 min read

Top 50 Python Interview Questions for Data Analyst
Common Python questions for DA interview
by Ruslan Shudra
Data Scientist
Apr, 2024・27 min read

The 50 Top SQL Interview Questions and Answers
For Junior and Middle Developers
by Oleh Lohvyn
Backend Developer
Apr, 2024・31 min read

Contenuto di questo articolo