What is the Open-Closed Principle?
Understand the Open-Closed Principle with a Java discount-calculator example and how it relates to the Strategy pattern.
Expected Interview Answer
The Open-Closed Principle (OCP) states that software entities — classes, modules, functions — should be open for extension but closed for modification, meaning new behavior should be added by writing new code, not by editing existing, tested code.
It is the "O" in SOLID, originally coined by Bertrand Meyer. In practice, OCP is achieved through abstraction: a stable interface or abstract base class defines the contract, and new functionality is added by creating new classes that implement it, while the code that consumes the abstraction never needs to change. This reduces regression risk because working, already-tested code stays untouched, and it is the mechanism behind extensible plugin systems, strategy-based algorithms, and framework hook points.
- Reduces regression risk in stable, tested code
- New features added via new classes, not edits to old ones
- Enables plugin-style extensibility
- Keeps consuming code decoupled from concrete implementations
AI Mentor Explanation
A stadium’s floodlight control system is built once against a generic "lighting fixture" interface, so when the venue upgrades from halogen to LED fixtures, electricians simply install new fixtures implementing that same interface — nobody touches the control system’s wiring or software. The system was closed for modification (the core control logic never changes) but open for extension (new fixture types slot straight in). That is exactly what OCP asks of a well-designed class hierarchy.
Step-by-Step Explanation
Step 1
Extract a stable abstraction
Identify the varying behavior and define an interface or abstract class that captures its contract.
Step 2
Implement variants as new classes
Each new behavior becomes a new class implementing the abstraction, never an edit to an existing class.
Step 3
Consume the abstraction, not concretes
Client code depends on the interface type, so it works with any current or future implementation.
Step 4
Verify no existing code changed
Confirm the feature was added purely by addition — no modification to already-tested classes was required.
What Interviewer Expects
- A precise definition: open for extension, closed for modification
- A concrete example showing extension via new classes, not edited ones
- Understanding that abstraction (interfaces/abstract classes) is the enabling mechanism
- Connection to reduced regression risk in production systems
Common Mistakes
- Vaguely saying "don’t change code" without explaining the extension mechanism
- Not connecting OCP to interfaces/abstract classes as the implementation technique
- Confusing OCP with encapsulation or information hiding
- Missing a concrete example when asked to demonstrate it in code
Best Answer (HR Friendly)
“The Open-Closed Principle says software should be designed so new features can be added by writing new code, not by changing code that already works and is already tested. In practice this means coding against interfaces or abstract classes so new behavior can be plugged in as a new class, which keeps the system stable and lowers the risk of breaking things.”
Code Example
interface DiscountRule {
double apply(double price);
}
class PercentageDiscount implements DiscountRule {
private double percent;
PercentageDiscount(double percent) { this.percent = percent; }
public double apply(double price) { return price - (price * percent / 100); }
}
class FlatDiscount implements DiscountRule {
private double amount;
FlatDiscount(double amount) { this.amount = amount; }
public double apply(double price) { return Math.max(0, price - amount); }
}
// PriceCalculator never changes when a new discount type is added
class PriceCalculator {
double calculate(double price, DiscountRule rule) {
return rule.apply(price);
}
}
// Adding a new discount type: BuyOneGetOneDiscount implements DiscountRule { ... }
// PriceCalculator above requires zero modification to support it.Follow-up Questions
- How does the Open-Closed Principle relate to the Strategy pattern?
- Can you give a real example where violating OCP caused a regression?
- How do abstract classes versus interfaces affect how OCP is applied?
- How does OCP interact with the other SOLID principles in practice?
MCQ Practice
1. The Open-Closed Principle states that software entities should be?
OCP is precisely: open for extension (new behavior can be added), closed for modification (existing tested code stays untouched).
2. The primary technique used to satisfy OCP is?
Coding against a stable interface or abstract class lets new implementations extend behavior without modifying consumers.
3. Which is a sign that OCP is being violated?
Needing to edit an existing conditional/switch every time a new case is added means the code is not closed for modification.
Flash Cards
What does OCP stand for? — Open-Closed Principle — open for extension, closed for modification.
What mechanism enables OCP? — Abstraction — coding against interfaces or abstract classes so new behavior extends without editing existing code.
What is a sign OCP is violated? — Having to edit an existing switch/if-else chain every time new behavior is added.
Which design pattern is closely tied to OCP? — Strategy pattern — new algorithms are added as new classes without modifying the context.