how-to-implement-interface-in-python

Introduction

The concept of interface is not explicitly defined in Python. Interface in Python is an abstract class that has only an abstract method in it but doesn’t have a single concrete method in it. Same as an abstract class the child class that is derived from the parent(abstract) class should define the abstract method in it and the parent class is derived from the ABC class of the abc module.

 

Python code

from abc import ABC, abstractmethod

class Parent(ABC):
    @abstractmethod
    def display(self):
        pass
    
    @abstractmethod
    def message(self):
        pass

class Children(Parent):
    def display(self):
        print("Display function redefined in child class.")
        
    def message(self):
        print("Message function redefined in child class.")
        
obj = Children()
obj.display()
obj.message()

Output

Display function redefined in child class.
Message function redefined in child class.

Description

The Parent class is derived from the ABC class of the abc module. Inside the Parent class, we declared two abstract methods in it. Then a child class is derived from the Parent class and those two abstract methods are redefined inside it. This is how one can create an interface in python. An abstract class with only abstract methods in it is labeled as an interface in python.

 

Things to keep in mind working with an interface

a) You can not create an object of abstract class

from abc import ABC, abstractmethod

class Parent(ABC):
    @abstractmethod
    def display(self):
        pass
    
    @abstractmethod
    def message(self):
        pass

obj = Parent()

Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-f6c33955da15> in <module>
     10         pass
     11 
---> 12 obj = Parent()

TypeError: Can't instantiate abstract class Parent with abstract methods display, message

 

b) You can not have any concrete method inside the abstract class. You won’t get any syntax error if you add a concrete method inside an abstract class working with an interface but it will be technically incorrect as an interface can not have concrete methods in it.

 

c) If you inherit a class from a class that has an abstract method in it then the redefinition of the abstract class should be given in the child class. Otherwise, that child class will also be an abstract class.

from abc import ABC, abstractmethod

class Parent(ABC):
    @abstractmethod
    def display(self):
        pass
    
    @abstractmethod
    def message(self):
        pass

class Children(Parent):
    def display(self):
        print("Display function redefined in child class.")
        
obj = Parent()
obj.display()
obj.message()

Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-85cc33a673d4> in <module>
     14         print("Display function redefined in child class.")
     15 
---> 16 obj = Parent()
     17 obj.display()
     18 obj.message()

TypeError: Can't instantiate abstract class Parent with abstract methods display, message

Since the redefinition of the abstract method “message” was not given we got an error. We should provide the redefinition of all the abstract methods of Parent class.

 

Use case of interface

If you want to create an application using the concept of Object-Oriented Programming(OOP) where the objects don’t share common properties, you can use an interface. Let’s see an example

from abc import ABC, abstractmethod

class School(ABC):
    @abstractmethod
    def Name(self):
        pass
    
    @abstractmethod
    def Id(self):
        pass

class Teacher(School):
    
    def __init__(self, id, name):
        self.id = id
        self.name = name
        
    def Name(self):
        print("Name: ", self.name)
    
    def Id(self):
        print("Id: ", self.id)
        
class Student(School):
    def __init__(self, id, name):
        self.id = id
        self.name = name
        
    def Name(self):
        print("Name: ", self.name)
    
    def Id(self):
        print("Id: ", self.id)
            
teach = Teacher(5, "Valentino")
teach.Name()
teach.Id()

print()

stu = Student(10, "Vitserk")
stu.Name()
stu.Id()

Output

Name:  Valentino
Id:  5

Name:  Vitserk
Id:  10

Here objects of both class Student and class Teacher don’t share common properties. So, in this case, we can use the concept of interface.

 

Conclusion

Interface in python is not explicitly defined in python. The interface is an abstract class that has only abstract methods but not a single concrete method. If the objects don’t share common properties while developing an application we can use the concept of interface.

If you want to learn more about the abc module click here

Happy Learning 🙂

Leave a Reply

Your email address will not be published.