Python Abstract Classes


Abstract Classes in Python

Abstract classes are a key concept in Object-Oriented Programming (OOP) that provide a blueprint for other classes. They allow you to define methods that must be created within any child classes built from the abstract class, promoting a consistent interface across different implementations. Abstract classes cannot be instantiated directly, meaning you cannot create an object of an abstract class. Instead, they are designed to be subclassed.

Key Concepts of Abstract Classes

  1. Abstract Method: A method that is declared but contains no implementation. It must be implemented by any concrete (non-abstract) subclass.
  2. Abstract Base Class (ABC): In Python, abstract classes are typically created using the abc module, which provides the ABC class and the abstractmethod decorator.

Creating an Abstract Class

Here’s how to create and use an abstract class in Python:

Example of Abstract Classes

from abc import ABC, abstractmethod # Abstract class class Shape(ABC): @abstractmethod def area(self): # Abstract method pass @abstractmethod def perimeter(self): # Abstract method pass # Concrete class inheriting from Shape class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height def area(self): # Implementing the abstract method return self.width * self.height def perimeter(self): # Implementing the abstract method return 2 * (self.width + self.height) # Concrete class inheriting from Shape class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): # Implementing the abstract method return 3.14 * (self.radius ** 2) def perimeter(self): # Implementing the abstract method return 2 * 3.14 * self.radius # Creating instances of concrete classes rectangle = Rectangle(5, 10) circle = Circle(7) # Calling methods print("Rectangle Area:", rectangle.area()) # Output: Rectangle Area: 50 print("Rectangle Perimeter:", rectangle.perimeter()) # Output: Rectangle Perimeter: 30 print("Circle Area:", circle.area()) # Output: Circle Area: 153.86 print("Circle Perimeter:", circle.perimeter()) # Output: Circle Perimeter: 43.96

Explanation of the Example

  1. Abstract Class Shape: This class is defined as abstract by inheriting from ABC. It contains two abstract methods, area and perimeter, which have no implementation. Any subclass must implement these methods.

  2. Concrete Classes Rectangle and Circle: These classes inherit from the abstract class Shape and provide implementations for the area and perimeter methods. Since they implement all the abstract methods, they can be instantiated.

  3. Instantiation: Instances of Rectangle and Circle are created, and their methods are called, demonstrating polymorphism. The exact implementation of the area and perimeter methods varies based on the specific shape.

Summary

  • Abstract Classes: Serve as blueprints for other classes and cannot be instantiated directly.
  • Abstract Methods: Methods defined in an abstract class that must be implemented by any concrete subclass.
  • Use of abc Module: The abc module provides tools to create abstract classes and methods, enforcing the implementation of specified methods in subclasses.

Abstract classes are a powerful tool in OOP, enabling you to define a consistent interface and enforce the implementation of specific methods across different subclasses, promoting code clarity and maintainability!