Stop 05 · Inheritance, Polymorphism & InterfacesTopics · 8/9

SOLID Principles

Part of the Java Roadmap

In short

SOLID comes up in almost every Java design interview, and more importantly it explains why some codebases are easy to change while others break when you touch them.

SOLID is five design guidelines: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation and Dependency Inversion. They help keep object-oriented code easy to change and test.

The jaadu, wearing a chef's cap, directs a tidy restaurant kitchen where each cook works at a separate station with a single dish.

Think of it like this

A well-run restaurant kitchen

The tandoor cook makes rotis, the curry cook makes gravies (one job each). New dishes join the menu without rewriting old recipes. Any trained chef can cover a shift. Waiters carry only the order slips they need. And the head chef orders "25 kg rice from a supplier", not from one specific shop. Those five habits are S, O, L, I and D.

Watch it happen

The five letters

S
O
L
I
D

1/5Single Responsibility: a class should have one reason to change. Billing maths and sending SMS are two reasons.

The big ideas

What you need to know

S and O keep changes local

Small, focused classes plus extension through new classes mean a change touches one file, not ten.

L catches bad inheritance

If a subclass throws UnsupportedOperationException for a parent method, it probably shouldn't be a subclass.

I keeps interfaces small

Split a fat interface into Printable and Scannable. Classes implement only what they truly support.

D makes testing easy

When CheckoutService receives a Notifier, a test can pass a fake one that just records messages.

See it in code

Walk through the code

java
1interface Notifier {2    void send(String to, String message);3}4 5class SmsNotifier implements Notifier {6    @Override7    public void send(String to, String message) {8        System.out.println("SMS to " + to + ": " + message);9    }10}11 12class InvoiceCalculator {13    long total(long subtotal, int gstPercent) {14        return subtotal + subtotal * gstPercent / 100;15    }16}17 18class CheckoutService {19    private final InvoiceCalculator calculator;20    private final Notifier notifier;21 22    CheckoutService(InvoiceCalculator calculator, Notifier notifier) {23        this.calculator = calculator;24        this.notifier = notifier;25    }26 27    void checkout(String phone, long subtotal) {28        long total = calculator.total(subtotal, 18);29        notifier.send(phone, "Order placed. Total Rs " + total);30    }31}

1/4A small interface (I). Checkout depends on this, not on SMS details (D).

new CheckoutService(new InvoiceCalculator(), new SmsNotifier()).checkout("+91 98765 43210", 1000) sends a total of Rs 1180.

Save yourself the pain

Common mistakes

Oops

Building a God class like OrderManager that calculates, saves, emails and prints.

Do this instead

Split it by reason to change: calculator, repository, notifier, printer. Each one is small and testable.

Oops

Writing new SmsNotifier() inside CheckoutService.

Do this instead

Ask for a Notifier in the constructor. The caller decides which one, and tests can pass a fake.

Oops

Treating SOLID as rules to apply everywhere from day one.

Do this instead

They are guidelines. Use them when code starts to hurt, not to add interfaces for a 20-line program.

Your mission

Fix a messy class

~30 min

Quick check

A Square class extends Rectangle, but setting its width also changes its height, breaking code that expects rectangles. Which principle is violated?

Go deeper

  1. 1Find a class in your projects that does too many jobs and split it.
  2. 2Write one small Java example for each of the five principles.
  3. 3Explain how Dependency Inversion connects to the dependency injection used by Spring.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge