SOLID in Java
SOLID is five design rules for objects: one job per type, open to extension, substitutable subtypes, small interfaces, and dependencies on abstractions. In Java they show up as class and package boundaries.
Detailed Explanation
SOLID is a mnemonic, not a framework. Each letter names a failure mode you will hit in a growing Java codebase.
S — Single responsibility. A class should change for one reason. UserService that validates emails, writes SQL, and sends mail changes whenever any of those policies change. Split it: a validator, a repository, a mailer. The class that remains orchestrates. Packages can follow the same idea: billing does not import ui.
O — Open/closed. You should add behaviour without editing a pile of if branches that already work. In Java that is usually polymorphism: a Fee interface and new implementations, not a growing switch on enum plus a new else if. Sealed types are the exception that proves the rule: when the set is closed, editing the switch is the point.
L — Liskov substitution. A subclass must be usable wherever the parent is. Square extends Rectangle with setters that keep sides equal breaks callers who set width and height independently. Prefer a Shape interface and two types. Throwing UnsupportedOperationException from an inherited method is a substitution failure unless the parent documented the method as optional (List.add on an unmodifiable list is the JDK living with that tension).
I — Interface segregation. Do not force implementors to stub methods they do not use. A Worker interface with work, eat, and sleep hurts a Robot. Three smaller interfaces let a class implement only what it does. This is why Closeable and Flushable are separate, and why a 40-method service interface is a maintenance trap.
D — Dependency inversion. High-level code should depend on interfaces, not on PostgresUserRepository. Pass the repository in. Tests pass a fake. Spring's constructor injection is this rule with a container. You can do it with new in a single composition root and still invert the rest of the graph.
You reach for SOLID when a class becomes hard to test or when a change in one feature breaks an unrelated one. You do not apply every letter to a 20-line script. A record with two fields does not need an interface. Use the rules as a review checklist, not as a requirement to create five types for every feature.
The cost of cargo-cult SOLID is a forest of one-method interfaces and factories. The cost of ignoring it is a god class that nobody can change safely. Keep types small, inherit only when substitutable, and construct graphs from the outside.
Code Examples
interface Mailer {
void send(String to, String body);
}
class Welcome {
private final Mailer mailer; // D: depend on an abstraction
Welcome(Mailer mailer) {
this.mailer = mailer;
}
void greet(String email) {
mailer.send(email, "welcome");
}
public static void main(String[] args) {
Mailer log = (to, body) -> System.out.println(to + ":" + body);
new Welcome(log).greet("a@b.com"); // prints: a@b.com:welcome
}
}interface Priced {
int paise();
}
class Catalog {
int total(Priced[] items) {
int sum = 0;
for (Priced item : items) {
sum += item.paise(); // O: new item types do not edit this loop
}
return sum;
}
}
record Book(int paise) implements Priced {}
record Pen(int paise) implements Priced {}
public class Totals {
public static void main(String[] args) {
System.out.println(new Catalog().total(new Priced[] {
new Book(500), new Pen(20)
})); // prints: 520
}
}Common Gotchas
One-method interfaces for every class are not required
extract an interface when a second implementation or a test double appears.
`extends` that throws `UnsupportedOperationException` fails L
wrap the type and expose the methods you support.
Unmodifiable `List` implementations already bend L
the interface documents optional operations. Your own types should not copy that without the same documentation.
Hidden `new PostgresRepo()` inside a domain class
the high-level policy now depends on a database driver. Pass the repo in.