What are the SOLID principles?
Tier: EssentialDifficulty: EasyAsked at: meesho
SOLID is five principles for writing object oriented code that's easy to change and test, and each letter names one.
- Single Responsibility, a class should have exactly one reason to change, one job.
- Open/Closed, a class should be open to extension but closed to modification, add new behavior without editing existing, working code.
- Liskov Substitution, a subclass should be usable anywhere its parent type is expected, without breaking anything.
- Interface Segregation, don't force a class to implement methods it doesn't need, split fat interfaces into smaller, focused ones.
- Dependency Inversion, depend on abstractions, not concrete implementations, so high level code doesn't know or care which specific class it's talking to.
None of these are Android specific, they're general object oriented design guidance, but Android code leans on them constantly, a ViewModel that only handles UI state and delegates data work to a Repository is Single Responsibility, ViewModelProvider.Factory letting you extend how a ViewModel gets built without touching the framework is Open/Closed, and a Repository interface with an in memory fake for tests versus a real Room backed implementation in production is Dependency Inversion in action. They're guidelines, not laws, the value is in the trade offs they force you to notice, not in following each one rigidly everywhere.