Extensibility Through Abstractions
What you'll learn: How to use interfaces and abstract classes to add new features without breaking existing code.
The Core Idea
Extensibility means your software can grow with new capabilities without requiring you to rewrite what already works. Abstractions—interfaces and abstract classes—are the key tools that make this possible. They let you define what something should do without locking in how it does it.
Think of a power outlet: it's an abstraction. Any device with the right plug can use it. You can buy a new lamp next year, and it'll work with the same outlet—no rewiring needed. The outlet defines the contract (voltage, plug shape), and each device implements that contract differently. Your house is extensible because it programs to the interface (outlet), not the implementation (specific lamp model).
How It Works
When you design with abstractions:
- Define the contract - Create an interface or abstract class that declares what operations are available
- Depend on the abstraction - Write your code to use the interface, not concrete implementations
- Add new implementations freely - Create new classes that fulfill the contract without touching existing code
If your payment system depends on an IPaymentProcessor interface instead of a specific CreditCardProcessor class, you can later add PayPalProcessor or CryptoProcessor without modifying any code that processes payments. You're extending behavior by addition, not modification.
Why This Matters
This approach directly supports the "Program to an Interface" principle you've learned. It makes your system open for extension but closed for modification—you add new capabilities by creating new implementations of existing abstractions, keeping your stable code stable.
Key Takeaway: Abstractions (interfaces and abstract classes) create extension points in your system, allowing new behavior to plug in without disrupting existing, working code.