Explanation
- An **interface** in Python is typically implemented using an **abstract base class (ABC)**. It defines a contract for subclasses: they must implement the methods defined in the interface. An interface only specifies method signatures, but doesn't provide any implementation.
- Python doesn’t have explicit interfaces like Java or C#. Instead, we use **abstract classes** with abstract methods to mimic the behavior of interfaces.
-
Steps
- Define an abstract base class with abstract methods using
ABCandabstractmethod. - Create subclasses that inherit the abstract base class and implement the abstract methods.
from abc import ABC, abstractmethod
class Vehicle(ABC): # Interface (Abstract Base Class)
@abstractmethod
def start(self):
pass
class Car(Vehicle): # Concrete class
def start(self):
print("Car is starting.")
car = Car()
car.start() # Output: Car is starting.