Python Interfaces: Defining Contracts for Class Implementations

thumb_up 1  ·  sell Interface definition in Python, Python interfaces, Defining contracts for class implementations in Python

In software engineering, an interface is a software architectural pattern. An interface is like a class but its methods just have prototype signature definition without any body to implement. The recommended functionality needs to be implemented by a concrete class.

In languages like Java, there is interface keyword which makes it easy to define an interface. Python doesn't have it or any similar keyword. Hence the same ABC class and @abstractmethod decorator is used as done in an abstract class.

An abstract class and interface appear similar in Python. The only difference in two is that the abstract class may have some non-abstract methods, while all methods in interface must be abstract, and the implementing class must override all the abstract methods.

Example

from abc import ABC, abstractmethod class demoInterface(ABC): @abstractmethod def method1(self): print ("Abstract method1") return @abstractmethod def method2(self): print ("Abstract method1") return

The above interface has two abstract methods. As in abstract class, we cannot instantiate an interface.

   obj = demoInterface()
         ^^^^^^^^^^^^^^^
TypeError: Can't instantiate abstract class demoInterface with abstract methods method1, method2

Let us provide a class that implements both the abstract methods. If doesn't contain implementations of all abstract methods, Python shows following error −

   obj = concreteclass()
         ^^^^^^^^^^^^^^^
TypeError: Can't instantiate abstract class concreteclass with abstract method method2

The following class implements both methods −

class concreteclass(demoInterface): def method1(self): print ("This is method1") return def method2(self): print ("This is method2") return obj = concreteclass() obj.method1() obj.method2()

Output

When you execute this code, it will produce the following output −

This is method1
This is method2

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back