An abstract class is a class that contains one or more abstract methods. An Abstract method is a method that generally doesn’t have any implementation, it is left to the sub classes to provide implementation for the abstract methods. Here note that in Python, abstract method can have implementation in the abstract class too but the sub class inheriting that abstract class is still forced to override the abstract methods.
Abstract class in Python
In Python abstract class is created by deriving from the meta class ABC which belongs to the abc (Abstract Base Class) module.
Syntax for creating abstract class in Python
from abc import ABC Class MyClass(ABC):
From abc module ABC meta class has to be imported and abstract class has to inherit ABC class in order to be considered as an abstract class.
Abstract method in Python
For defining abstract methods in an abstract class method has to be decorated with @abstractmethod
decorator. From abc module @abstractmethod decorator has to be imported to use that annotation.
Syntax for defining abstract method in abstract class in Python is as given below.
from abc import ABC, abstractmethod Class MyClass(ABC): @abstractmethod def mymethod(self): #empty body pass
Important points about abstract class in Python
- Abstract class can have both concrete methods as well as abstract methods.
- Abstract class works as a template for other classes. Using an abstract class you can define a generalized structure without providing complete implementation of every method. Methods which provide common functionality to be used for all derived classes are defined as concrete methods in abstract class where as the methods where implementation may vary are defined as abstract methods.
- Abstract class can’t be instantiated so it is not possible to create objects of an abstract class.
- Generally abstract methods defined in abstract class don’t have any body but it is possible to have abstract methods with implementation in abstract class. Any sub class deriving from such abstract class still needs to provide implementation for such abstract methods.
- If any abstract method is not implemented by the derived class Python throws an error.
Python abstract class example
In the example there is an abstract class with a concrete method common and an abstract method vary. Two sub classes Child1 and Child2 inherit the abstract class Parent and implement the abstract method vary.
from abc import ABC, abstractmethod class Parent(ABC): #common functionality def common(self): print('In common method of Parent') @abstractmethod def vary(self): pass class Child1(Parent): def vary(self): print('In vary method of Child1') class Child2(Parent): def vary(self): print('In vary method of Child2') # object of Child1 class obj = Child1() obj.common() obj.vary() # object of Child2 class obj = Child2() obj.common() obj.vary()
Output
In common method of Parent In vary method of Child1 In common method of Parent In vary method of Child2
As you can see when common method is called the common() method of Parent is invoked. When vary method is called, method defined with in the subclass is invoked.
Python abstract class with abstract method implemented
Even if you provide implementation of abstract method in abstract class sub class is still forced to implement the abstract method.
from abc import ABC, abstractmethod class Parent(ABC): #common functionality def common(self): print('In common method of Parent') @abstractmethod def vary(self): print('In vary method of Parent') class Child(Parent): pass # object of Child1 class obj = Child() obj.common() obj.vary()
Output
Traceback (most recent call last): File "F:/NETJS/NetJS_2017/Python/Test/Parent.py", line 18, in <module> obj = Child() TypeError: Can't instantiate abstract class Child with abstract methods vary
If you implement vary() method in subclass then it works fine.
from abc import ABC, abstractmethod class Parent(ABC): #common functionality def common(self): print('In common method of Parent') @abstractmethod def vary(self): print('In vary method of Parent') class Child(Parent): def vary(self): print('In vary method of Child') # object of Child class obj = Child() obj.common() obj.vary()
Output
In common method of Parent In vary method of Child
Not implementing all abstract methods
If subclass doesn’t implement all the abstract methods defined in the inherited abstract class that also results in an error.
from abc import ABC, abstractmethod class Parent(ABC): #common functionality def common(self): print('In common method of Parent') @abstractmethod def vary(self): pass @abstractmethod def test(self): pass class Child(Parent): def vary(self): print('In vary method of Child') # object of Child class obj = Child() obj.common() obj.vary()
Output
Traceback (most recent call last): File "F:/NETJS/NetJS_2017/Python/Test/Parent.py", line 17, in <module> obj = Child() TypeError: Can't instantiate abstract class Child with abstract methods test
Instantiating abstract class in Pyhton
Since abstract class are supposed to have abstract methods having no body so abstract classes are not considered as complete concrete classes, thus it is not possible to instantiate an abstract class.
from abc import ABC, abstractmethod class Parent(ABC): def __init__(self, value): self.value = value; super().__init__() #common functionality def common(self): print('In common method of Parent', self.value) @abstractmethod def vary(self): pass class Child(Parent): def vary(self): print('In vary method of Child') # trying to instantiate abstract class obj = Parent(3) obj.common() obj.vary()
Output
Traceback (most recent call last): File "F:/NETJS/NetJS_2017/Python/Test/Parent.py", line 18, in <module> obj = Parent(3) TypeError: Can't instantiate abstract class Parent with abstract methods vary
That's all for this topic Abstract Class in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Tutorial Page
Related Topics
You may also like-