What Is an Abstract Class in Python

What is an abstract class in Python

An abstract method is a method that has a declaration but no implementation. A class with at least one of these abstract methods is called an abstract class in Python.

To create an abstract class in Python, use the abc module.

For example, here is an abstract class Pet:

from abc import ABC, abstractmethod

class Pet(ABC):
   @abstractmethod
   def makesound(self):
      pass

When you inherit an abstract class, the child class should define all the abstract methods present in the parent class.

Notice that you cannot create objects of an abstract class. An abstract class can only act as a blueprint for its subclasses.

Why Write Abstract Classes in Python

An abstract class acts as a blueprint for subclasses. It is the main class that specifies how its child classes should behave.

For example, Shape could be an abstract class that implements an abstract method area(). Then the subclasses Circle, Triangle, and Rectangle provide their own implementations to this method based on their characteristics in geometry.

How to Write Abstract Classes in Python

To write an abstract class in Python, you need to use the abc (Abstract Base Class) module.

  • Import ABC class and abstractmethod decorator from the abc module.
  • Make your abstract class be a subclass of ABC class. This indicates it is going to be an abstract class.
  • Mark your method with an @abstractmethod decorator to make it an abstract method.

To make any sense of this, let’s see an example.

Abstract Class Example in Python

To demonstrate how to use abstract classes in Python, let’s see a simple example.

In this example, we implement a base class Pet. In this class, we implement an abstract method makesound().

Why abstract?

Because a different pet makes a different sound, so it is up to the type of pet what sound it is supposed to make. All we care about in the abstract parent class is that the subclass implements the makesound() method no matter how.

Here is the code:

from abc import abstractmethod

class Pet():
   @abstractmethod
   def makesound(self):
      pass

class Cat(Pet):
   def __init__(self, name):
       self.name = name

   def makesound(self):
      return "Meow!"

cat = Cat("Max")
print (f"I like to say {cat.makesound()}")

Output:

I like to say Meow!

And if you like to, you can verify that it is indeed impossible to create an object from the abstract class.

For example:

pet = Pet()

This results in an error:

TypeError: Can't instantiate abstract class Pet with abstract method makesound

Conclusion

Today you learned what is an abstract class in Python.

An abstract class is a base class that specifies what methods the subclasses should implement. You cannot create an object from an abstract class.

Thanks for reading. I hope you enjoy it.

Happy coding!

Further Reading

Python Interview Questions and Answers

Useful Advanced Features of Python

Share on facebook
Share on twitter
Share on linkedin