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 is without any implementation.

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

The following section will demonstrate how we create and use abstract classes in Python.

Implement A subclass – No abstract class as a base class-

Is any class with a function with no body code 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? Our class, MyAbstractClass, 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 ensures that there can not be any instance of an abstract class, and a base 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 the 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 constraint that an abstract class can not be instantiated.

Code – Where a Derived class does not implement all base class methods.

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