Abstract Class in Python

Video Tutorial
Class & Object in Python thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Topics Covered


Abstraction is the concept in object-oriented programming that is used to hide the internal functionality of the classes from the users. Abstraction is implemented using the abstract classes. An abstract class in Python is typically created to declare a set of methods that must be created in any child class built on top of this abstract class. Similarly, an abstract method is one that doesn't have any implementation.

Introduction to Python Abstract Classes

In abstraction, the users are familiar with the purpose of the class's methods, but they don't know how they solve the purpose, which means that they know the inputs and expected outputs, but the inner working is hidden.

There are plenty of real-life examples, like when we press a button on a TV remote to change the channel; we don’t know how it does that. We are just interested in the fact that when we press a particular button, it changes the channel.

Smartphones are another example of abstraction; we are unaware of any of the internal functionalities of the phone. We are just concerned with what we need to do to do a task.

For example, we don’t know how the phone records a video by pressing the record button, we just touch/press a button, and it does that. There are numerous other examples of abstraction for us to observe in the real world.

We can deduce from the above examples that abstraction helps us make everything more user-friendly and less complex. In the context of programming, Abstraction is used to make the life of other developers easy. For example, in Python we do not know how the sort() function of the list class works, we just use this function, and it sorts a list for us.

We cannot create an abstract class in Python directly. However, Python does provide a module that allows us to define abstract classes. The module we can use to create an abstract class in Python is abc(abstract base class) module.

Abstract methods force the child classes to give the implementation of these methods in them and thus help us achieve abstraction as each subclass can give its own implementation. A class containing one or more than one abstract method is called an abstract class.

We can use the following syntax to create an abstract class in Python:

Here we just need to inherit the ABC class from the abc module in Python.

Now, let's take the following example to demonstrate abstract classes:

To define an abstract method we use the @abstractmethod decorator of the abc module. It tells Python that the declared method is abstract and should be overridden in the child classes.

We can use the following syntax to create an abstract method in Python:

We just need to put this decorator over any function we want to make abstract, and the abc module takes care of the rest.

Now, let's take following example to demonstrate abstract classes:

We’ll see detailed usage of this module in the next sections of this article.

Python Abstract Class Example

Now, we have seen how to create an abstract class in Python. Let’s take a very common example to illustrate the use of abstract classes and methods.

Let’s say we want to draw different shapes. To do this, we create a basic abstract class Shape which would contain the blueprint for specific shapes to follow. This class will provide the methods that are needed to be implemented by its child classes for specific shapes.

Let’s create the Shape class first:

As you may say that we have declared an undefined method draw(). This method will be implemented by the other classes that inherit this class.

For example, If we want to draw a circle shape. We create a class Circle that will inherit the Shape class and implement the draw() method. Let's see the implementation of the Circle class:

Now we can create an object of this class in our main app.py and call its draw() method:

The output of the above code is:

Now, let's say we want to draw another shape, say a Triangle. We create a class Triangle similar to the Circle class that will inherit the Shape class and implements the draw() method. Let’s see the implementation of the Triangle class:

Now, let's create an object of both shapes in our main app.py file and call their draw() methods.

The output of the above code as expected is:

Similarly, we can create as many specific shape classes as we want, with the Shape class being the main abstract class.

We'll see how this helps us in the next sections.

Why Use Abstract Base Classes?

As we have discussed above, abstract classes are used to create a blueprint of our classes as they don't contain the method implementation. This is a very useful capability, especially in situations where child classes should provide their own separate implementation. Also, in complex projects involving large teams and a huge codebase, It is fairly difficult to remember all the class names.

Importance of Abstract Classes

As we’ve discussed in the above section, we define a blueprint for our classes using an abstract class. The importance of using abstract classes in Python is that if our subclasses don’t follow that blueprint, Python will give an error. Thus we can make sure that our classes follow the structure and implement all the abstract methods defined in our abstract class.

Let’s take an example to understand this. Suppose in our example of the Circle class, we do not define the draw() method; instead, we define a draw_circle() method, which is doing the same thing as the draw().

Let’s see what happens if we do that and run our main app.py program:

The output of the above code is:

So, by using the abstract classes, we can ensure that our subclasses use the same structure and same method names for similar tasks.

Also, by using the abstract classes, we can hide unnecessary details from the user and reduce the programming complexity to a great extent.

Don't miss the chance to join our free Python course and receive a recognized certification upon completion.

Abstract Properties

Abstract class in Python also provides the functionality of abstract properties in addition to abstract methods. Properties are Pythonic ways of using getters and setters. The abc module has a @abstractproperty decorator to use abstract properties. Just like abstract methods, we need to define abstract properties in implementation classes. Otherwise, Python will raise the error.

As we have been told, properties are used in Python for getters and setters. Abstract property is provided by the abc module to force the child class to provide getters and setters for a variable in Python.

Let’s define an abstract property in our Shape class:

Now, let’s try to create the object of the Circle class:

If we run this piece of code, we’ll get the following error:

The reason we get this error is that we do not have any property named name in the concrete class Circle. Let’s rectify this by defining this property in the Circle class:

Now, let’s try to create the object of the Circle class again and access the name property:

This time, we'll get the following output:

Abstract Class Instantiation

Abstract classes are not complete, as they may have some methods that are not defined. So we cannot create an instance or object of an abstract class in Python. If we try to instantiate the abstract class, it raises an error.

Let’s verify this by trying to instantiate our Shape class in our app.py file:

Now when we run this code, we'll get the following error:

Invoke Methods from Abstract Classes

Unlike some other programming languages, abstract Python methods don’t need to be completely abstract; they can have some basic-level implementation that can be used by all the concrete classes. A Concrete class is a class that has a definition for all its methods and has no abstract method.

Concrete classes can use the super() to call the base abstract method and do some additional tasks into it. In our example, the abstract method draw() of the Shape class could be used to prepare a basic canvas, and then concrete classes could work on top of that to draw specific shapes. Let’s see this in the code:

As you can see, instead of declaring an empty method, we have defined high-level implementation in the draw() method.

Let’s see the Circle class now:

Here, we have first called the draw() method of the Shape class through super().draw() and then provided the additional implementation required.

Let’s try to call the draw() method of Circle class again:

This time, we'll get the following output:

In this way, we can provide a common high-level implementation for all the subclasses of the abstract base class, and those subclasses can provide their own implementation. Use this Compiler to compile your Python code.

How do Abstract Base Classes Work?

We have already seen how abstract classes are inherited by concrete subclasses. They define generic methods using the @abstractmethod decorator and properties using @abstractproperty that force the subclasses to implement those methods and properties. Thus, implementation is handled by the concrete subclasses where we can create objects to perform tasks.

Implementation through Subclassing

Throughout this article, we have implemented the abstract base classes through the use of subclassing. We first inherit the ABC class in the abstract class(Shape), and then we inherit this abstract class in the concrete or child class(Circle). This is the recommended and best way to implement abstract classes.

There is also a second way of implementing abstract classes by registering the classes manually as parents and children. But this manual process of registering the class is more complex as well as less intuitive, and because of this most of the developers implement abstract classes through subclassing.

Concrete Methods in Abstract Base Classes

The abstract classes may also contain concrete methods that have the implementation of the method and can be used by all the concrete classes. To define a concrete method in an abstract class, we simply define a method with implementation and don’t decorate it with the @abstractmethod decorator. If needed, we may also override this concrete method in the concrete class to provide any additional functionality as per user needs.

Let’s have a look at the following code snippet to create a concrete method in the abstract class(Shape):

In the above code, we have defined a concrete method in the Shape class called print_greetings() that will just print a simple message. We can now invoke this method using the object of our concrete class(Circle):

The output of the above code is:


  • We can make abstract classes in Python using abc module of Python
  • We can implement abstract classes by inheriting the ABC class
  • We use @abstractmethod decorators to define abstract methods in Python
  • Abstract classes help to provide the blueprint for another class
  • Abstract classes may contain concrete methods
  • Abstract classes cannot be instantiated
  • Abstract classes ensure that our software is flexible enough to support future changes