Abstract Class and Abstract Method in Python

Abstract Class and Abstract Method

In this post, we will discuss Abstract Class and Abstract Method. We know that a class is nothing but a blueprint that contains attributes and actions for creating objects or instances. When we create an object for any class, the object also gets access to the attributes and methods of the class. We also know that all the objects can access any method present in a class.

Suppose several classes must inherit a class in a Python program, and each child class wants to have a different definition of each method available in the parent class. In that case, it is better to have the parent class with empty methods or just placeholders so that the child classes can define the methods as per their needs in the code block of the respective child classes. This concept leads us to the usage of Abstract Classes in Python.

  • Abstract classes can have usual methods with definitions and abstract methods without any definition.
  • It is called an interface if an abstract class has only abstract methods.

Let us look at a program now with only one abstract method.

#abstract class example
from abc import ABC, abstractclassmethod
class SampleClass(ABC):
  @abstractclassmethod
  def calculate(self):
    pass

#first subclass inheriting SampleClass
class SubSampleClass1(SampleClass):
  def calculate(self,a):
    print("Cube value: ",a*a*a)

#second subclass inheriting SampleClass
class SubSampleClass2(SampleClass):
  def calculate(self,a):
    print("Square value: ",a*a)

import math
class SubSampleClass3(SampleClass):
  def calculate(self,a):
    print("Square root value: ",math.sqrt(a))

#creating objects or instances
inst1 = SubSampleClass1()
inst2 = SubSampleClass2()
inst3 = SubSampleClass3()
inst1.calculate(3)
inst1.calculate(8)
inst3.calculate(625)
Output

Cube value:  27
Cube value:  512
Square root value:  25.0

In the program above, we created the class SampleClass as an abstract superclass that has an abstract method within it called calculate(). You can notice that this method does not have any declaration or definition within it. This class is an example of an abstract class with only an abstract method. We mentioned the inbuilt decorator from abc module called, @abstractmethod above the calculate() method in Sample class

The other classes inherit this abstract class in the program, and the empty calculate() method gets available to other classes. The other classes also have a calculate() method that overrides the calculate() method of the abstract class, SampleClass. Each subclass’s calculate() method has a different logic per the requirement.

We also used ‘from abc import ABC, abstractmethod’ at the program’s top. This code means we are importing ABC and abstractmethod from the abc module. The ‘abc‘ module stands for the abstract base class module.

Since we should extract all the abstract classes from the meta class ABC available in the abc module, we should the code, ‘from abc import ABC, abstractmethod‘ at the beginning of the program.

Let us look at a real-world example of the abstract class and method implementation that also has a normal method within it.

from abc import * #importing the abc module
class Bike(ABC):
  def __init__(self,bike_no):
    self.bike_no = bike_no
  
  def fillAir(self):
    print("Fill air into the tire")
    print("For the bike with Bike Number: ",self.bike_no)
  
  @abstractmethod
  def digital(self):
    pass

  @abstractmethod
  def pillionseat(self):
    pass

#create another class and inherit the abstract class Bike
class Bullet(Bike):
  def digital(self):
    print("Some bullet bikes are not digital")
    print("But they are known for something else")
  def pillionseat(self):
    print("Some bikers don't prefer to have a pillion seat")
    print("To add more beauty to the bullet bike")

#create object for the Bullet class
b = Bullet(1234)
b.fillAir()
b.digital()
b.pillionseat()
Output:

Fill air into the tire
For the bike with Bike Number:  1234
Some bullet bikes are not digital
But they are known for something else
Some bikers don't prefer to have a pillion seat
To add more beauty to the bullet bike

From the program above, you can notice that the abstract class, Bike, also has a usual method fillAir() with a certain logic to print the output. It means that abstract classes can also have normal methods along with abstract methods in abstract classes.

Points to be noted on Abstract Class and Abstract Method

  • An abstract class can contain abstract methods with empty definitions and a regular method with a definition, as shown in the examples above.
  • If an abstract class has only abstract methods in it, then the abstract class is called an interface. Please refer to the next chapter in our Python tutorial series to know more differences between an interface and abstract class in Python.
  • All abstract classes need to be derived from the abc module. Therefore, we import this module at the beginning of the program that requires abstract classes.
Scroll to Top