Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Interfaces

Define contracts with interfaces for flexible, maintainable code.

Video Explanation

Interfaces in OOP Explained

Watch on YouTube

Interfaces define contracts that classes must follow. They specify what methods a class must implement without specifying how they should be implemented. This allows for polymorphism and loose coupling.

An interface is a contract that specifies what methods a class must implement. It defines the “what” but not the “how”.

  • Polymorphism - Different classes can be used interchangeably
  • Loose Coupling - Depend on abstractions, not concrete classes
  • Flexibility - Easy to swap implementations
  • Testability - Easy to create mock implementations
  • Multiple Inheritance - Classes can implement multiple interfaces

Python doesn’t have explicit interfaces like Java, but uses:

  • Abstract Base Classes (ABC) - Similar to interfaces
  • Protocols - Structural subtyping (duck typing)
  • ABC with @abstractmethod - Enforces method implementation

Classes can implement multiple interfaces:

Diagram

Use interfaces when:

  • You want to define a contract that multiple classes can follow
  • You need polymorphism - treat different classes the same way
  • You want loose coupling - depend on abstractions
  • You need multiple inheritance of behavior (not state)
  • You want to make code more testable with mock implementations

Examples:

  • Payment processors (different payment methods)
  • Notification services (email, SMS, push)
  • Data access layers (different databases)
  • Storage services (local, cloud, database)
  • Authentication providers (OAuth, JWT, etc.)

💡 Time to Practice!

Now that you understand the concepts, put them into practice with our interactive playground. Build UML diagrams, write code, and get AI-powered feedback.

Browse All Problems