Sunday, February 9, 2020

Abstract Classes in Python

Abstract Classes In Python


Abstract Class


Abstract classes are used to implement abstraction in Python like many other object-oriented programming languages. Sometimes, it is not possible for a class to have the complete implementation details, but it is possible that the subclasses of this class can be implemented completely. We can declare such class as abstract class.

Abstract Method


An abstract class contains abstract methods(a method without implementation detail). An abstract method is a method that has a declaration part but does not contain any implementation part. The abstract classes cannot be instantiated, it means it is not possible to create an object of an abstract class.

 An abstract class provides us the details of the interface only(What methods must be in subclasses and what must be their behavior). In other words, A class having partial implementation only is known as an abstract class. A class having complete implementation is known as a concrete class.



Abstract class in Python
Abstract Class Example

Why do we need Abstract classes?


The abstract classes allow us to provide some default functionality for some classes. We can define an API(application program interface) the set of some classes with the help of abstract classes, as abstract classes only define the interface which will be defined by the abstract class. The capability of abstract classes is particularly useful when some third party implementation needs to be provided such as plugins, extensions, and drivers.


How to create Abstract classes in Python?


Python does not provide any by default support for abstract classes but there is a provision to enable Python with abstract classes. Abstract classes in Python are provided by a special module, known as ABC which is the acronym for Abstract Base Classes. It means for creating the abstract classes in Python we need to import module ABC.

We can work with the module ABC by marking the methods of the base class as abstract method and then declaring concrete classes as the implementation of the Abstract base. A Method can be declared as abstract with keyword @abstractmethod decorator.

Creating an Abstract Class




Creating the object of an abstract class should be illegal, but this is not the case here, The code below will work fine,

from abc import ABC, abstractmethod

class A(ABC):
  def m(self):
    pass

  def m1(self):
    print('doing good')

o=A()                 #legal
o.m1()                #Legal

Output:
doing good

@abstractmethod


The instantiation of an abstract class object is not possible, for this we need to use @abstractmethod 
so that we can restrict the instantiation of an abstract class.

For example


from abc import ABC, abstractmethod
class A(ABC):
  @abstractmethod
  def m(self):
    pass
  def m1(self):
    print('doing good')

o=A()                 #legal
o.m1()                #Legal

Output:
Traceback (most recent call last):
  File "/tmp/sessions/5c0f75831a995535/main.py", line 9, in <module>
    o=A()                 #legal
TypeError: Can't instantiate abstract class A with abstract methods m