Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / SOLID Principles

    Quick Reference

    Glossary and cheat sheet for SOLID Principles — the page to reread before an interview.

    Extra: Reference4 min read

    Glossary#

    TermPlain meaningChapter
    cohesionhow well a class's parts belong together (want high)1
    couplinghow much a class depends on others' details (want low)1
    SRPone reason to change per class2
    OCPextend by adding code, not by editing it3
    strategy (pattern)interchangeable behaviour behind an interface3
    LSPsubclasses must be substitutable for their parent4
    ISPmany small interfaces over one fat one5
    DIPbusiness logic and details both depend on abstractions6
    dependency / dependency injectionwhat a class needs / passing it in from outside6
    constructor injectiondependencies passed through the constructor6
    composition rootthe place that builds and wires objects6
    fake / test doublea stand-in implementation used in tests6
    DRY / KISS / YAGNIone source of truth / keep it simple / don't build for imagined needs7
    Law of Demetertalk to direct collaborators, not chains of strangers7

    Cheat Sheet#

    Output
      S  "describe it without 'and'?"                 → split by reason to change
      O  "add the next variant without editing?"      → interface + new implementation (strategy)
      L  "would a parent-type caller be surprised?"   → fix hierarchy; compose instead
      I  "does anyone fake/throw on a method?"        → split the interface
      D  "does business logic `new` up I/O?"          → interface + constructor injection
      Always inject: database, HTTP, email, file system, clock, randomness
      Balance with: KISS · YAGNI · refactor when change actually arrives
    Java
    interface Notifier {
        void send(String to, String msg);
    }
    
    class QuizService {
        private final Notifier notifier;
        private final Clock clock; // java.time.Clock
    
        QuizService(Notifier notifier, Clock clock) {
            this.notifier = notifier;
            this.clock = clock;
        }
    }
    // prod:  new QuizService(new EmailNotifier(), Clock.systemUTC())
    // test:  new QuizService(new FakeNotifier(), Clock.fixed(Instant.parse("2026-11-20T10:00:00Z"), ZoneOffset.UTC))

    Try This Yourself#

    1. Refactor QuizManager. Write Part 0's QuizManager as one class, then refactor it step by step into ScoreCalculator, ReportFormatter, ResultRepository (an interface with an in-memory implementation), Notifier and CreditRule. After each step, name the principle you applied.

    2. Add three features without editing old classes: a JSON formatter, a "teacher plan" credit rule, and an SMS notifier. If you had to edit an existing class, find which principle was missing.

    3. LSP detective. Look at java.util.List.of(1, 2, 3).add(4) — run it and read the exception. Explain in two sentences whether this is an LSP concern and why Java accepted the trade-off.