An interface is a blueprint that defines what methods a class must implement, without providing their implementation.
Do Interfaces Exist in Python?
Python does not have a built-in interface keyword like Java.
However, Python implements interfaces using:
-
Abstract Base Classes (ABC)
-
Abstract methods
Why Interfaces are Needed ?
- enforce method implementation
- achieve loose coupling
- support multiple inheritance
- build scalable applications
- define contracts between classes
Example :
Python
from abc import ABC, abstractmethod class Vehicle(ABC): @abstractmethod def start(self): pass class Car(Vehicle): def start(self): print("Car started") class Bike(Vehicle): def start(self): print("Bike started") c = Car() b = Bike() c.start() b.start() #output Car started Bike started Interface vs Abstract Class :
| Interface | Abstract Class |
|---|---|
| Only abstract methods | Abstract + concrete methods |
| No implementation | Partial implementation |
| Used for contracts | Used for base logic |
| Multiple inheritance | Limited inheritance |