Implementing abstract classes
Hey there! Today, we’re going to learn about abstract classes in Python. Imagine you’re playing with Legos, and you have different types of Lego blocks: some are square, some are round, and some are triangular. An abstract class is like a blueprint for a Lego block, but you can’t build anything with just the blueprint. You need to create specific blocks based on that blueprint.
An abstract class in Python is a class that cannot be instantiated (you can’t create an object directly from it). It is meant to be a base class that other classes inherit from and provide implementations for its methods.
Why Use Abstract Classes?
- Consistency: They ensure that certain methods are implemented in all subclasses.
- Design: They provide a clear structure for your code, making it easier to understand and maintain.
Creating Abstract Classes in Python
In Python, we use the ‘abc‘ module to create abstract classes. The ‘abc‘ module stands for “Abstract Base Classes.” Let’s see how to do this step by step.
- Import the abc Module:
from abc import ABC, abstractmethod
- Define an Abstract Class:
class Animal(ABC):
@abstractmethod
def sound(self):
pass
Here, ‘Animal‘ is an abstract class with an abstract method ‘sound‘. The ‘@abstractmethod‘ decorator tells Python that ‘sound‘ is an abstract method and must be implemented by any subclass of ‘Animal'.
- Create Subclasses:
class Dog(Animal):
def sound(self):
return "Woof!"
class Cat(Animal):
def sound(self):
return "Meow!"
Now, ‘Dog‘ and ‘Cat' are subclasses of ‘Animal‘, and they provide their own implementation of the ‘sound‘ method.
Putting It All Together
Let’s put everything together in a complete example:
from abc import ABC, abstractmethod
# Abstract class
class Animal(ABC):
@abstractmethod
def sound(self):
pass
# Subclass implementing the abstract method
class Dog(Animal):
def sound(self):
return "Woof!"
# Another subclass implementing the abstract method
class Cat(Animal):
def sound(self):
return "Meow!"
# Create instances of the subclasses
dog = Dog()
cat = Cat()
# Print the sounds
print(dog.sound()) # Output: Woof!
print(cat.sound()) # Output: Meow!
Practice Questions
Now it’s your turn to practice!
- Question 1: Create an Abstract Class Create an abstract class called ‘
Shape‘ with an abstract method ‘area‘. Then, create two subclasses ‘Circle‘ and ‘Square‘ that implement the ‘area‘ method. - Question 2: Implement and Instantiate Implement the ‘
Shape‘ class with ‘Circle‘ and ‘Square‘ subclasses. Create instances of ‘Circle‘ with radius 3 and ‘Square‘ with side 4, and print their areas.
Solutions to Practice Questions
- Solution 1: Create an Abstract Class
from abc import ABC, abstractmethod
# Abstract class
class Shape(ABC):
@abstractmethod
def area(self):
pass
- Solution 2: Implement and Instantiate
from abc import ABC, abstractmethod
import math
# Abstract class
class Shape(ABC):
@abstractmethod
def area(self):
pass
# Subclass implementing the abstract method for Circle
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * (self.radius ** 2)
# Subclass implementing the abstract method for Square
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side ** 2
# Create instances of the subclasses
circle = Circle(3)
square = Square(4)
# Print the areas
print(circle.area()) # Output: 28.274333882308138
print(square.area()) # Output: 16
In this solution, we created an 'Shape' abstract class with a ‘area‘ method. Then, we implemented ‘Circle' and ‘Square‘ subclasses provided their own ‘area’ method implementations and printed the areas of instances.
That’s it! Now you know how to create and use abstract classes in Python. Happy coding!
