Java Reference ยท Object-Oriented Concepts
SOLID principles in Java
The five SOLID design principles illustrated with small Java examples: single responsibility, open-closed, Liskov substitution, interface segregation and dependency inversion.
SOLID is a mnemonic for five object-oriented design guidelines popularized by Robert C. Martin. They are not laws; they are heuristics that tend to produce code that is easier to change and test. Each one targets a typical source of pain: classes that do too much, code that must be edited for every new case, subclasses that break their parents' promises, fat interfaces, and hard-wired dependencies.
This page gives each principle a short statement, a before-and-after in Java, and the trade-offs. Apply them where change actually happens, not everywhere in advance.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
The five principles
| Principle | Statement | Typical Java tool |
|---|---|---|
| Single responsibility | A class should have one reason to change. | Small classes, packages by feature. |
| Open-closed | Add behavior by adding code, not by editing tested code. | Interfaces, strategy objects, sealed hierarchies. |
| Liskov substitution | Subtypes must honor the contract of their supertypes. | Preconditions no stronger, results no weaker; composition over inheritance. |
| Interface segregation | Clients should not depend on methods they do not use. | Several small interfaces instead of one large one. |
| Dependency inversion | High-level code depends on abstractions, not on concrete low-level classes. | Constructor injection, Supplier, interfaces. |
Usage notes
- S is about reasons to change, not number of methods. A class can have ten methods and one responsibility.
- O in practice means polymorphism: a
switchon a type code that keeps growing is a hint. With sealed types and pattern matching, a closedswitchis also fine when the set of cases really is fixed and the compiler checks it. - L is violated when a subclass throws
UnsupportedOperationExceptionfor an inherited method, strengthens preconditions, or changes the meaning of a method.List.of(...).add(...)is a well-known example in the JDK of an immutable collection implementing a mutable interface. - I is why
Runnable,ComparableandSupplierhave exactly one method. Default methods can add convenience without forcing implementers. - D is not the same as dependency injection frameworks. Passing a collaborator through a constructor is enough, and it makes unit tests trivial.
- Over-application produces needless interfaces with one implementation and deep layers. Introduce an abstraction when you have a second use or a clear test need.
- They conflict sometimes. Splitting classes for S can scatter logic; weigh that against readability.