Skip to main content
PreparationZone Logo
PrepZone

SOLID in Practice

Five principles as five questions to ask about a class, with the refactor each one suggests.

Pass checkpoint below

Why this matters

  • These five names come up in every design discussion and most interviews, and reciting the acronym without an example is transparently shallow.
  • Each principle has a concrete code smell attached, which makes them usable while you type rather than only during review.
  • Applied mechanically they produce over-engineered code, so knowing when not to apply one is part of knowing them.

The five at a glance

Single responsibilityOne reason to change
Open / closedExtend, do not edit
Liskov substitutionSubtype keeps the promise
Interface segregationSmall, focused contracts
Dependency inversionDepend on abstractions
ResultSwappable, testable units
Read them as questions about a single class: does it have one job, can I extend it without editing it, and does it depend on contracts rather than concrete types?

Single Responsibility

The question: if this class had to change, how many different reasons could cause it?

A class should have one reason to change — one group of stakeholders whose decisions affect it.

Java
Loading compiler…
Java
Loading compiler…

Open/Closed

The question: to add a new case, do I have to edit existing code or only add new code?

Open for extension, closed for modification. The smell is a growing if/else or switch on a type.

Java
Loading compiler…
Java
Loading compiler…

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 5 sections

  • Liskov Substitution
  • Interface Segregation
  • Dependency Inversion
  • Three more worth the same attention
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.