🔹 Introduction
In programming, sometimes we want to create a general idea of something but not an actual object. For example, the concept of a Vehicle is abstract—you cannot directly use it, but you can use specific types like Car or Bike. Abstract classes in Python work in the same way. They are used to define rules for other classes but cannot be used to make objects directly.
⚙️ What is an Abstract Class?
An abstract class is a special type of class that:
- Cannot be used to create objects directly
- Can contain abstract methods (methods with no body, only a declaration)
- Can also contain normal methods (methods with code that work normally)
Abstract classes help us define methods that must be implemented in child classes. This ensures that all subclasses follow the same structure.
In Python, abstract classes are created using the abc module (Abstract Base Classes).
🧑💻 How to Create an Abstract Class in Python
To create an abstract class:
- Import the ABC class from the abc module.
- Use the @abstractmethod decorator for methods that should be abstract.
Example:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start_engine(self):
pass
def stop_engine(self):
print("Engine stopped")
👉 Here, Vehicle is an abstract class because it has an abstract method start_engine().
🚗 Using Abstract Classes
If a child class inherits from an abstract class, it must implement all abstract methods. If it doesn’t, Python will give an error.
Example:
class Car(Vehicle):
def start_engine(self):
print("Car engine started")
class Bike(Vehicle):
def start_engine(self):
print("Bike engine started")
car = Car()
car.start_engine() # Output: Car engine started
bike = Bike()
bike.start_engine() # Output: Bike engine started
👉 Both Car and Bike implement the start_engine() method as required.
📌 Why Do We Use Abstract Classes?
Abstract classes are useful because they:
- Enforce rules: All child classes must implement the abstract methods.
- Provide a common blueprint: All subclasses share a similar structure.
- Avoid code duplication: Common methods can be written in the abstract class.
For example, if all vehicles must have a start_engine() method, an abstract class ensures that every child class (Car, Bike, Bus, etc.) implements it.
🏗️ Abstract Classes with Multiple Methods
Abstract classes can contain more than one abstract method. In such cases, the child class must implement all of them.
Example:
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
rect = Rectangle(5, 10)
print("Area:", rect.area()) # Output: Area: 50
print("Perimeter:", rect.perimeter()) # Output: Perimeter: 30
👉 The Rectangle class implements both abstract methods: area() and perimeter().
🏁 Summary
Abstract classes in Python are like blueprints that define rules for other classes. They are created using the ABC module and contain abstract methods that child classes must implement. This ensures consistency, avoids repetition, and makes the code more organized. By using abstract classes, developers can build structured and maintainable programs where every subclass follows the same rules.