Course contentsShow
← Software Engineering
Lesson 204 of 1,9336. SOLID PrinciplesFree lesson

Extension Through Interfaces and Polymorphism

Discover how interfaces enable adding new implementations without changing code that depends on abstractions.

Extension Through Interfaces and Polymorphism

You'll learn how interfaces let you add new features by creating new implementations, without touching existing code that depends on abstractions.

The Core Idea

We've seen how inheritance allows extension. But interfaces combined with polymorphism offer an even more powerful way to follow the Open/Closed Principle: you can add entirely new behaviors by implementing an interface differently, and the code using that interface doesn't need to change at all.

Think of a payment processor in an app. Instead of building specific logic for credit cards, then modifying it for PayPal, then modifying it again for cryptocurrency, you define a PaymentMethod interface. Each payment type implements that interface. Your checkout code works with the interface—it doesn't know or care which specific implementation it receives. When you need to support Apple Pay next month, you simply create a new class implementing PaymentMethod. Zero changes to checkout logic.

Why This Matters

When code depends on an interface rather than concrete classes, you can extend by:

  • Creating new classes that implement the interface
  • Passing different implementations at runtime (polymorphism)
  • Never modifying the code that uses the interface

The abstraction (interface) stays stable. The implementations vary. This is extension without modification—the heart of Open/Closed.

Polymorphism means "many forms"—the same interface reference can point to different implementations, and the correct behavior happens automatically.

Key Takeaway: Interfaces enable extension through new implementations rather than modification of existing code, making your system open for extension but closed for modification through polymorphism.