How to Create an Abstract Class in Python

How to Create an Abstract Class in Python ? : 3 Steps Only

Abstract Class is a blueprint for other classes. You can easily restrict derived classes to define and implement the Abstract methods of the base class. If you look into the interfaces and abstract class then there is one simple difference that makes the abstract class better than the interface.  You cannot define function implementation inside the interface method but in the abstract class, you can also define the implementation part inside the base class. In this entire tutorial of “How to,” you will learn to create an abstract class in Python?

In python programming language by default abstract class is not available. But you can do it with the help of a module “ABC” module that is available in python.  This makes the methods inside the Base Class as an Abstract method that must be implemented.

How to Create an Abstract Class in Python?

Step 1: Import the necessary module

from abc import ABC, abstractmethod

Step 2: Create an Abstract Base Class

I have created a Professional abstract base class.  with the fun1().

class Professional(ABC):
    @abstractmethod
    def fun1(self):
        pass

Here I am calling decorator @abstractmethod for the method I want to implement.

Step 3: Create Derived Class

Lets now create Derived Classes from the base class Professional.

Case 1: Not implementing the function

When you are not implementing the function of the base class in the derived class then you will get the TypeError after the object instantiation. It means you must have to implement the function to remove the error.

class Doctor(Professional):
    pass

Error :

not implemented method error

Case 2: Implementing the method

class Doctor(Professional):
    def fun1(self):
        print("I am",__class__.__name__)

 

One thing you will notice that when you create a derived class from the base class and will not implement the methods then you will get the underline dotted signal ( in Pycharm). It means there are some functions you must have to implement. In addition, you can create your own functions inside the derived class.

class Doctor(Professional):
    def fun1(self):
        print("I am",__class__.__name__)
    def fun2(self):
        print("This is function 2")

d = Doctor()
d.fun1()
d.fun2()

Output
doctor derived class

In the same way, you can create as many derived classes from the base Class Professional. And you must have to implement all the methods defined in the Base Class.

Full Code

from abc import ABC, abstractmethod


class Professional(ABC):
    @abstractmethod
    def fun1(self):
        pass


class Doctor(Professional):
    def fun1(self):
        print("I am", __class__.__name__)

    def fun2(self):
        print("This is function 2")


class Engineer(Professional):
    def fun1(self):
        print("I am", __class__.__name__)


class Teacher(Professional):
    def fun1(self):
        print("I am", __class__.__name__)


d = Doctor()
d.fun1()
d.fun2()
e = Engineer()
e.fun1()
t = Teacher()
t.fun1()

Output

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner