categories.language Intermediate
What Do SOLID Principles Stand For? Can You Give Examples?
SOLID Principles
S — Single Responsibility Principle
A class should have only one reason to change
- Bad:
Userclass handles business logic, database access, and email sending - Good: Split into
User,UserRepository,EmailService
O — Open/Closed Principle
Open for extension, closed for modification
- Add new features without modifying existing code; use inheritance/composition
- Strategy Pattern is a classic implementation
L — Liskov Substitution Principle
Subclasses must be substitutable for their base class without breaking behavior
- Violation:
Square extends Rectangle—setting width also sets height, breaking rectangle semantics
I — Interface Segregation Principle
Don't force clients to depend on interfaces they don't use; split large interfaces into small, specific ones
D — Dependency Inversion Principle
High-level modules shouldn't depend on low-level modules; both should depend on abstractions (interfaces)
- Dependency Injection (DI) is the common technique to apply this principle
Interview bonus: SOLID is a guideline, not a rigid rule. Over-applying it (e.g., adding interfaces everywhere) increases complexity. Balance flexibility against simplicity.
✦ AI Mock Interview
Type your answer and get instant AI feedback
Sign in to use AI scoring
