What are the SOLID principles?
MediumConceptOOP & SOLID
Answer
SOLID is a set of five object-oriented design principles that make code easier to extend, test and maintain.
- Single Responsibility: a class should have one reason to change; keep invoice calculation separate from invoice printing.
- Open/Closed: open for extension, closed for modification; add a new
PaymentMethodsubclass instead of editing a growingif-elsechain. - Liskov Substitution: a subtype must work wherever its base type is expected without breaking behaviour; a
Squarethat changes both sides when only the width is set breaks code written forRectangle. - Interface Segregation: prefer small, focused interfaces so that clients do not depend on methods they do not use.
- Dependency Inversion: high-level modules depend on abstractions rather than concrete classes, typically supplied through constructor injection.
More in OOP & SOLID
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What are the four pillars of object-oriented programming? | Concept | Easy | Read the answer to What are the four pillars of object-oriented programming? | |||
| 2 | What is the difference between an abstract class and an interface? | Concept | Easy | Read the answer to What is the difference between an abstract class and an interface? | |||
| 3 | What are the SOLID principles? | Concept | Medium | Read the answer to What are the SOLID principles? | |||
| 4 | Why is composition often preferred over inheritance? | Concept | Medium | Read the answer to Why is composition often preferred over inheritance? |