abstract class in Python

What is Abstract Class In Python?


In Python, you can imagine an abstract class as a blueprint of the actual class. It contains one or more abstract methods. An abstract method only has a signature and has no implementation.

You can not create an object of an abstract class. A programmer needs to develop a subclass to implement the abstract methods and then he can instantiate the subclass.

In the following section, we will demonstrate, how we create and use abstract classes in Python.

Implement A subclass – No abstract class as base class-

Is any class that has a function with no body code is an abstract class? Let’s check with the below example.

class MyAbstractClass:
     #function have no implementation
     def abstract_method(self):
         pass

 #Derived class from base class  MyAbstractClass

 class SubClass(MyAbstractClass):
     pass

# Creating Objects of Both the above classes.
 BaseClassOnject  = MyAbstractClass()
 DerivedClassObject = SubClass();
 print("Successfully Executed ");

If you run the above code, it will execute successfully. Why so? We have a class MyAbstractClass, which has one method with no implementation. Isn’t it an abstract class?

The answer is no, it is an example of inheritance and has nothing to do with an abstract class.

Abstract Base Class (ABC) Module?

The abc is a module in python for creating abstract classes. An abstract class inherits the ABC class from the module. This makes sure that there can not be any instance of an abstract class and a drived class from an abstract class should implement all of its functions. That forces a clear hierarchy of the code and completeness of the implementation.

How to create an abstract class?

Following is the python code example for creating an abstract class for Car service.

from abc import ABC, abstractmethod
 class VechileCheck(ABC):       
     @abstractmethod
     def EngineCheck(self):
         pass
     @abstractmethod
     def AcCheck(self):
         pass
     @abstractmethod
     def GearBoxCheck(self):
         pass
#Creating a Drived Class
 class BMWService(VechileCheck):
     pass
d = VechileCheck();
 x = BMWService();

Output->

Traceback (most recent call last):
   File "AbstractClass.py", line 18, in 
     d = VechileCheck();
 TypeError: Can't instantiate abstract class VechileCheck with abstract methods AcCheck, EngineCheck, GearBoxCheck

The output shows the constrain that an abstract class can not be instantiated.

Code – Where a Derived class do not implements all methods of base class.

class BMWService(VechileCheck):
 def EngineCheck(self):     
print("Engine Check done for BMW");     
pass 
def AcCheck(self):     
print("AC Check done for BMW");     
pass
 x = BMWService();

Output->

Traceback (most recent call last):
   File "AbstractClass.py", line 25, in 
     x = BMWService();
 TypeError: Can't instantiate abstract class BMWService with abstract methods GearBoxCheck

Full Code example-

from abc import ABC, abstractmethod
 class VechileCheck(ABC):       
     @abstractmethod
     def EngineCheck(self):
         pass
     @abstractmethod
     def AcCheck(self):
         pass
     @abstractmethod
     def GearBoxCheck(self):
         pass
 class BMWService(VechileCheck):
 def EngineCheck(self):     
print("Engine Check done for BMW");     
pass def AcCheck(self):     
print("AC Check done for BMW");     
pass def GearBoxCheck(self):     
print("AC Check done for BMW");     
pass
 class SKODAService(VechileCheck):
 def EngineCheck(self):     
print("Engine Check done for SKODA");     
pass 
def AcCheck(self):    
 print("AC Check done for SKODA");     
pass 
def GearBoxCheck(self):     
print("AC Check done for SKODA");     
pass
 x = BMWService();
 x.EngineCheck();
 x.AcCheck();
 x.GearBoxCheck();
 y = SKODAService();
 y.EngineCheck();
 y.AcCheck();
 y.GearBoxCheck();

Output->

Engine Check done for BMW
 AC Check done for BMW
 AC Check done for BMW
 Engine Check done for SKODA
 AC Check done for SKODA
 AC Check done for SKODA