FoundationsOOP Foundations·7 min revision·Updated 2026-10-29
SOLID Principles
The 5 foundational object-oriented design principles essential for scalable low-level system design.
#OOP#Design Principles#Clean Architecture
1-Minute Revision Card
SOLID PrinciplesCore Entities & Components
Single ResponsibilityOpen/ClosedLiskov SubstitutionInterface SegregationDependency Inversion
Architecture & Patterns
Strategy (OCP)Factory (DIP)Adapter (ISP)
Key Methods, APIs & Formulas
High-cohesion classesPolymorphic abstractionsInjected dependenciesExact Phrases to Say in the Interview
- “A class should have only one reason to change.”
- “Open for extension, closed for modification.”
- “Depend upon abstractions, not concretions.”
SOLID Principles
⚡ 1-Line Idea: Five guiding design principles that make software modular, testable, and maintainable under change.
1. Quick Revision Card
- S - Single Responsibility Principle (SRP): One class, one responsibility, one reason to change.
- O - Open/Closed Principle (OCP): Open for extension by adding new subclasses/strategies; closed for modification of existing tested code.
- L - Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types without breaking application invariants.
- I - Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use (lean, role-specific interfaces).
- D - Dependency Inversion Principle (DIP): High-level modules should depend on abstractions (interfaces), never concrete low-level implementations.
2. Real-Life Analogy
Think of a modular smartphone. The battery, camera module, and charging port communicate through standard interfaces (USB-C). You can swap in a higher-capacity battery (extension) without rewiring the motherboard (modification).
3. Bad vs Good Code (Open/Closed Principle Example)
Bad: Violating OCP with Type Checking
java
public class NotificationService {
public void send(String type, String message) {
if (type.equals("EMAIL")) {
System.out.println("Sending Email: " + message);
} else if (type.equals("SMS")) {
System.out.println("Sending SMS: " + message);
}
// Adding PUSH requires editing this tested method!
}
}Good: OCP with Polymorphic Strategy
java
public interface NotificationSender {
void send(String message);
}
public class EmailSender implements NotificationSender {
@Override
public void send(String message) {
System.out.println("Sending Email: " + message);
}
}
public class SmsSender implements NotificationSender {
@Override
public void send(String message) {
System.out.println("Sending SMS: " + message);
}
}
// Open for extension: new sender types plug in without changing NotificationService
public class NotificationService {
public void notifyUser(NotificationSender sender, String message) {
sender.send(message);
}
}4. How to Say it in an Interview
"I am applying the Open/Closed Principle here by introducing an interface so we can plug in new payment gateways without altering existing payment processing logic."
5. Memory Trick
S-O-L-I-D: Single job, Open plug, Legit swap, Interface lean, Depend on abstract.