Abstract Base Classes

Get introduced to abstract base classes in this lesson.

It is mandatory for a derived class to implement methods of a base class. In this lesson, we’ll see how the Python built-in abc module facilitates it.

Introduction

A class that contains one or more than one abstract method is called an abstract class. An abstract method is declared, but isn’t implemented. The abc module in the Python library provides the infrastructure for defining custom abstract base classes as sometimes it’s not possible to instantiate a base class.

This module works by marking methods of the base class as abstract. This is done by the @abstractmethod decorator. A subclass of an abstract base class then overrides its abstract methods. The abc module defines ABCMeta class, which is a metaclass for defining abstract base classes.

Explanation

Consider, there are two classes:

  • Base - An abstract base class
  • Derived - A concrete subclass

Let’s do some coding using the abc module.

Get hands-on with 1200+ tech skills courses.