Abstract Method in python

An Abstract method is a method which is declared but does not have implementation such type of methods are called as abstract methods.

In Python, we can declare an abstract method by using @abstractmethod decorator.

This abstract method is present in the abc module in python, and hence, while declaring the abstract method, we have to import the abc module compulsory.

Example:

from abc import abstractmethod
class Vehicle:
    @abstractmethod
    def getNoOfWheels(Self):
        pass

The above program does not have any implementation, and we won't get any output.

Here the Child class is responsible for providing an implementation for the parent class abstract method.

Python Operators

Abstract Classes in python

The partially implemented classes are called an abstract class; every abstract class in python should be a child of ABC class, which is present in the abc module.

Example:

from abc import ABC, abstractmethod
class Vehicle(ABC):
    @abstractmethod
    def getNoOfWheels(Self):
        pass

The child classes are responsible to provide implementation to parent class abstract methods.

The following example demonstrates the creation of parent abstract class defining an abstract method, which is partially implemented, creating child classes that are responsible for providing implementation to parent abstract class. These classes are called concrete classes.

We cannot create an object to the abstract class, but we can create an object to the child's class.

from abc import ABC, abstractmethod
class Vehicle(ABC):     ##abstarct class
    @abstractmethod   ##abstract method
    def getNoOfWheels(Self):
        pass
    
class Bus(Vehicle):   ##implementing parent abstract class by using child class
    def getNoOfWheels(self):
        return 6
    
class Auto(Vehicle):   ##implementing parent abstract class by using child class
    def getNoOfWheels(self):
        return 3
    
b=Bus()    
print(b.getNoOfWheels())    


a=Auto()
print(a.getNoOfWheels())

The output is:

implementing-abstract-parentclass

What is the advantage of declaring an abstract method in the parent class

Ans: By declaring an abstract method in the child class, we can provide some guidelines to the child classes, such that they should compulsorily implement those methods; otherwise, child class will take it as optional.

if we do not provide any implementation to that abstract method, then Python will throw an error

from abc import ABC, abstractmethod
class Vehicle(ABC):
    @abstractmethod
    def getNoOfWheels(Self):
        pass
    
class Bus(Vehicle):
   pass
    
b=Bus()

The output is:

without-providing-implementation-to-the-abstract-method

If we remove the @abstractmethod decorator, then the method becomes a normal method and the child class may or may not give implementation to it.

from abc import ABC, abstractmethod
class Vehicle(ABC):
   
    def getNoOfWheels(Self):
        pass
    
class Bus(Vehicle):
   pass
    
b=Bus()

The output is: It is not giving any output, but it is not giving any error.

after-removinf-abstract-method-decorator

Python Modules

Points to be remembered on Abstract Methods and Classes in python

1.If a class containing one abstract method and if we are extending ABC class then instantiation id not possible; for Abstract class with an abstract method instantiation(creating an object) is not possible.

Consider the below example which is a concrete class that does not contain an abstract method and an abstract class, and hence we can perform instantiation/create an object

class Test:
    pass

t=Test()

The below example contains an abstract class but does not include an abstract method, and therefore, we can perform instantiation/can create an object.

An abstract class can contain a zero number of abstract methods
from abc import *
class Test(ABC):
    pass

t=Test()

And the below example includes one abstract class and abstract way, and hence, we cannot perform instantiation.

from abc import *
class Test(ABC):
    pass

t=Test()

##The output is:

  File "C:/Users/User/.spyder-py3/temp.py", line 6, in <module>
    t=Test()

TypeError: Can't instantiate abstract class Test with abstract methods m1

But, the following example contains an abstract method and does not include an abstract class, and hence we can perform instantiation.

from abc import *
class Test:
      @abstractmethod
      def m1(self):
          pass

t=Test()

2. If we are creating a child class for abstract class, then for every abstract method of parent class compulsory we should provide an implementation in the child class; otherwise child class also becomes an abstract class, and we cannot create an object for the child class.

The following example contains an abstract method with an abstract class, but when we create a child class for the abstract parent class, we have not implemented the parent abstract method, and hence python will throw an error that we cannot create an object for that.

from abc import *
class Test(ABC):
      @abstractmethod
      def m1(self):
          pass
      
class SubTest(Test):
    pass       

s=SubTest()

##The output:

  File "C:/Users/User/.spyder-py3/temp.py", line 10, in <module>
    s=SubTest()

TypeError: Can't instantiate abstract class SubTest with abstract methods m1

The following example contains an abstract class with two abstract methods when we create a child class, we are implementing only one abstract method of the parent class, and hence, we cannot perform instantiation.

from abc import *
class Test(ABC):
      @abstractmethod
      def m1(self):
          pass
      @abstractmethod
      def m2(self):
          pass
      
class SubTest(Test):
    def m1(self):
        print("m1 method")

s=SubTest()
s.m1()

##The output is:

  File "C:/Users/User/.spyder-py3/temp.py", line 14, in <module>
    s=SubTest()

TypeError: Can't instantiate abstract class SubTest with abstract methods m2

The following example contains an abstract class with two abstract methods, and in the child class, we are implementing both methods m1 and m2, and hence we can create an object, or we can perform instantiation.

from abc import *
class Test(ABC):
      @abstractmethod
      def m1(self):
          pass
      @abstractmethod
      def m2(self):
          pass
      
class SubTest(Test):
    def m1(self):
        print("m1 method implementation")
        
    def m2(self):
        print("m2 method implementation")

s=SubTest()
s.m1()
s.m2()

The output is:

creating-objects-for-two-abstract-methods

The following example contains an abstract class, which contains two methods one is an abstract method and another one is a non-abstract method, and the child class is responsible to provide an implementation to the non-abstract class, and it is already given an implementation also, and hence we can perform instantiation.

from abc import *
class Test(ABC):
      def m1(self):
          print("non-abstract method")
      @abstractmethod
      def m2(self):
          pass
      
class SubTest(Test):
    def m2(self):
        print("m2 method implementation")

s=SubTest()
s.m1()
s.m2()

The output is:

creating-an-object-two-thenon-abstract-method

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions