Interfaces in Python

Interfaces in Python with Examples

In this article, I am going to discuss Interfaces in Python with Examples. Please read our previous article where we discussed Abstract classes in Python. As part of this article, we are going to discuss the following pointers which are related to Interfaces in Python.

  1. What is Interface in Python
  2. When should we go for interfaces?
  3. When should we go for abstract class?
  4. When should we go for concrete class?
What is Interface in Python?

In our previous article, we discussed that an abstract class is a class which may contain some abstract methods as well as non-abstract methods also. Imagine there is an abstract class which contains only abstract methods and doesn’t contain any concrete methods, such classes are called Interfaces.

Therefore, an interface is nothing but an abstract class which can contains only abstract methods.

Points to Remember:

In python there is no separate keyword to create an interface. We can create interfaces by using abstract classes which have only abstract methods. Interface can contain:

  1. Constructors
  2. Variables
  3. abstract methods
  4. sub class

As we know, abstract methods should be implemented in the subclass of interface (Abstract Class). Object creation is not possible for the interface class (abstract class). We can create objects for the child class of interface to access implemented methods.

Program: Interface having two abstract methods and one sub class (demo11.py)

from abc import ABC, abstractmethod
class Bank(ABC):
   @abstractmethod
   def balance_check(self):
       pass
   @abstractmethod
   def interest(self):
       pass

class SBI(Bank):
   def balance_check(self):
       print("Balance is 100 rupees")
   def interest(self):
       print("SBI interest is 5 rupees")


s = SBI()
s.balance_check()
s.interest()

Output:

Interface having two abstract methods and one sub class in python

Program: one interface and two sub classes to that interface (demo12.py)

from abc import *
class DBInterface(ABC):
   @abstractmethod
   def connect(self):
       pass
   @abstractmethod
   def disconnect(self):
       pass

class Oracle(DBInterface):
   def connect(self):
       print('Connecting to Oracle Database...')
   def disconnect(self):
       print('Disconnecting to Oracle Database...')

class Sybase(DBInterface):
   def connect(self):
       print('Connecting to Sybase Database...')
   def disconnect(self):
       print('Disconnecting to Sybase Database...')   

dbname=input('Enter Database Name either Oracle or Sybase:')
classname=globals()[dbname]
x=classname()
x.connect()
x.disconnect()

Output:

one interface and two sub classes to that interface in python

The globals()[str] is a predefined function, it will convert the string ‘str’ into a class name and returns the classname.

When should we go for interfaces?

Since, Interfaces will not contain implemented methods, when we don‟t know anything about implementation of requirements, then we should go for interfaces.

When should we go for abstract class?

An abstract class is a class which can contains few implemented methods and few unimplemented methods as well. When we know about requirements partially, but not completely, then we should go for abstract class.

When should we go for concrete class?

Concrete class is a class which is fully implemented. It contains only implemented methods. When we know complete implementation about requirements, then we should go for concrete class.

Useful Information:

__str__() method:

This is one of the magical methods. Whenever we are trying to print any object reference internally, then this method will be called. It returns output, about the location of the object, in the string format as shown in the example below.

Program: __str__ method example in python (demo13.py)

class Student:
   def __init__(self, name, rollno):
       self.name=name
       self.rollno=rollno
s1=Student('Ram',10)
s2=Student('Rahim' ,12)
print(s1)
print(s2)

Output:

Interfaces in Python with Examples

We can override the __str__ method in our class, to return a meaningful string whenever we are trying to print an object.

Program: Overriding __str__ method example in python (demo14.py)

class Student:
   def __init__(self, name, rollno):
       self.name=name
       self.rollno=rollno

   def __str__(self):
       return 'This is Student object with name {} and roll no {}'.format(self.name, self.rollno)
s1=Student('Ram',10)
s2=Student('Rahim' ,12)
print(s1)
print(s2)

Output:

Interfaces in Python with Examples

In the next article, I am going to discuss Exception Handling in Python. Here, in this article, I try to explain Interfaces in Python with Examples. I hope you enjoy this Interfaces in Python with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.