Course contentsShow
← Software Engineering
Lesson 172 of 1,9335. Software Design PrinciplesFree lesson

Program to an Interface, Not an Implementation

Discover how depending on abstractions rather than concrete implementations enables flexibility and testability.

Program to an Interface, Not an Implementation

What you'll learn: How depending on abstractions rather than concrete implementations enables flexibility and testability in your software design.

The Core Idea

When you write code, you can either depend directly on specific concrete classes (implementations) or depend on abstract contracts (interfaces). "Programming to an interface" means your code should rely on what something can do rather than exactly how it does it.

Think of it like hiring someone for their skills rather than their specific identity. You need "someone who can drive a truck," not "Bob specifically." If Bob quits, anyone with driving skills can step in. Similarly, if your code depends on "something that can save data" rather than "specifically a MySQL database," you can swap in a different storage system without rewriting your entire application.

Why This Matters

When you program to an implementation, you create tight coupling—your code knows too much about the specific class it uses. If that class needs to change or you want to use a different implementation, you must modify all the dependent code.

Programming to an interface creates loose coupling. Your code only knows about the abstract contract (the interface), not the details. This makes your system:

  • Flexible: Swap implementations without changing dependent code
  • Testable: Easily substitute test doubles during testing
  • Maintainable: Changes to one implementation don't ripple through your codebase

This principle builds directly on Abstraction and Information Hiding (lesson 167) and Encapsulation (lesson 171), hiding implementation details behind stable contracts.

Key Takeaway: Depend on abstractions (interfaces) rather than concrete classes to create flexible, testable, and maintainable systems that adapt easily to change.