An interface defines the inputs and outputs of a thing.
Consider a kettle, for example: every kettle has an input for water, a button to start boiling the water, and an output for the same water. How exactly that gets implemented in a particular kettle is up to the kettle manufacturer.
This is different from Abstract Classes
For example, in the following code we are Creating a Python Interface with a Shape Abstract Class that serves as an OOP interfaces with the calculate_area method. and maintains good OOP Polymorphism
Note
This type of inheritance allows shapes to inherit interfaces rather than functionality.
# shapes_ocp.py
from abc import ABC, abstractmethod
from math import pi
class Shape(ABC):
def __init__(self, shape_type):
self.shape_type = shape_type
@abstractmethod
def calculate_area(self):
pass
class Circle(Shape):
def __init__(self, radius):
super().__init__("circle")
self.radius = radius
def calculate_area(self):
return pi * self.radius**2
class Rectangle(Shape):
def __init__(self, width, height):
super().__init__("rectangle")
self.width = width
self.height = height
def calculate_area(self):
return self.width * self.height
class Square(Shape):
def __init__(self, side):
super().__init__("square")
self.side = side
def calculate_area(self):
return self.side**2