SOLID Principles in Programming Explained with Examples
Writing clean, maintainable, and scalable code is one of the most important goals in software development. SOLID principles are a set of design guidelines that help developers achieve better code structure and flexibility.
SOLID is an acronym representing five key principles in object-oriented programming. These principles make code easier to understand, extend, and maintain.
What are SOLID Principles?
- S – Single Responsibility Principle (SRP)
- O – Open/Closed Principle (OCP)
- L – Liskov Substitution Principle (LSP)
- I – Interface Segregation Principle (ISP)
- D – Dependency Inversion Principle (DIP)
1. Single Responsibility Principle (SRP)
A class should have only one reason to change. This means a class should perform only one job or responsibility.
❌ Bad Example
class User {
saveUser() {}
sendEmail() {}
}
This class is handling both data storage and email logic.
✅ Good Example
class User {
saveUser() {}
}
class EmailService {
sendEmail() {}
}
Now each class has a single responsibility.
2. Open/Closed Principle (OCP)
Software entities should be open for extension but closed for modification.
❌ Bad Example
class Discount {
calculate(type) {
if (type === 'regular') return 10;
if (type === 'premium') return 20;
}
}
✅ Good Example
class Discount {
calculate() {}
}
class RegularDiscount extends Discount {
calculate() { return 10; }
}
class PremiumDiscount extends Discount {
calculate() { return 20; }
}
Now new discount types can be added without modifying existing code.
3. Liskov Substitution Principle (LSP)
Objects of a parent class should be replaceable with objects of its child class without breaking the application.
❌ Bad Example
class Bird {
fly() {}
}
class Penguin extends Bird {
fly() { throw "Can't fly"; }
}
✅ Good Example
class Bird {}
class FlyingBird extends Bird {
fly() {}
}
class Penguin extends Bird {}
Now substitution does not break behavior.
4. Interface Segregation Principle (ISP)
Clients should not be forced to depend on interfaces they do not use.
❌ Bad Example
interface Worker {
work();
eat();
}
✅ Good Example
interface Workable {
work();
}
interface Eatable {
eat();
}
Now classes only implement what they need.
5. Dependency Inversion Principle (DIP)
High-level modules should not depend on low-level modules. Both should depend on abstractions.
❌ Bad Example
class MySQLDatabase {}
class App {
db = new MySQLDatabase();
}
✅ Good Example
class Database {}
class MySQLDatabase extends Database {}
class App {
constructor(db) {
this.db = db;
}
}
Now the app depends on abstraction, not a specific database.
Why SOLID Principles Matter
- Improves code readability
- Makes code easier to maintain
- Enhances scalability
- Reduces bugs and technical debt
Real-World Use Case
Modern applications like large-scale backend systems use SOLID principles to design modular and scalable services. These principles are widely used in frameworks, APIs, and microservices architecture.
Final Thoughts
SOLID principles are essential for writing clean and maintainable code. By following these principles, developers can build systems that are flexible, scalable, and easy to extend.
If you are preparing for interviews or building real-world applications, mastering SOLID principles is a must.




