Skip to content

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

    Java / SOLID Principles

    Interview Questions

    Real interview questions on SOLID Principles, answered the way you would say them out loud.

    Extra: Interview prep5 min read

    Interview questions

    Explain SOLID with examples.

    How I'd answer: Go letter by letter, one sentence of definition and one concrete example each:

    • "S — one reason to change: I'd split a QuizManager that scores, formats, saves and emails into a calculator, formatters, a repository and a notifier.

    • O — open for extension, closed for modification: credit rules implement a CreditRule interface, so the exam-week promotion is a new class, not an edit to the billing method.

    • L — subclasses must be substitutable: the classic square-extends-rectangle breaks code that sets width and height independently — I measured area 16 instead of 20 — so I'd model them with a common Shape interface instead.

    • I — small interfaces: split a fat NoteStore into NoteReader and NoteWriter so a read-only cache doesn't have to throw on save.

    • D — depend on abstractions and inject them: QuizService takes a Notifier in its constructor, so it works with email, WhatsApp, or a fake in unit tests."

    Combines: Ch 2–6.

    What's the difference between dependency inversion and dependency injection?

    How I'd answer: "Dependency inversion is the design principle: high-level policy shouldn't depend on low-level details; both should depend on an abstraction — the service depends on a Notifier interface, not on an email library. Dependency injection is a technique for achieving it: instead of the service constructing its dependencies, they're supplied from outside, usually via the constructor. You can do DI by hand at the application's entry point, or let a framework like Spring or FastAPI's Depends do the wiring. DI without an interface still helps testability a little, but you get the full benefit when the injected type is an abstraction."

    Combines: Ch 6.

    Give an example of a Liskov violation you might see in real code.

    How I'd answer: "A common one is a subclass that can't support an inherited operation and throws UnsupportedOperationException — for example a read-only repository extending a repository interface that has save. Any code written against the parent type expects save to work, so substituting the subclass breaks it at runtime. Java's own List.of returns an immutable list whose add throws, which is a known trade-off in the library. The fix is usually interface segregation — separate read and write interfaces, so a read-only class only implements the reader — or composition instead of inheritance."

    Combines: Ch 4, 5.

    Isn't applying SOLID everywhere over-engineering?

    How I'd answer: "It can be. SOLID is about making likely changes cheap, and every abstraction has a cost in indirection. I start with the simplest design and apply the principles when change actually shows up — the second time I have to edit an if chain for a new variant, I extract a strategy interface. The exception is I/O: I inject databases, HTTP clients, email and the clock from day one, because that's what makes the code testable. YAGNI and KISS are the counterweights — I don't build extension points for changes nobody has asked for."

    Combines: Ch 7, 8.

    How would you make this class testable?" (shown a service that calls new PaymentClient() and LocalDateTime.now() inside its methods)

    How I'd answer: "The class creates its own dependencies, so a test can't avoid real payments and can't control the time. I'd introduce a PaymentGateway interface and a Clock — Java has java.time.Clock built in — and pass both through the constructor. In production, the entry point wires the real payment client and Clock.systemUTC(); in tests I pass a fake gateway that records calls and Clock.fixed(...) for a specific date. Then I can test rules like 'refunds are only allowed within 7 days' deterministically. That's dependency inversion plus constructor injection."

    Combines: Ch 6.