SOLID Principles

SOLID is a set of five object-oriented design principles, popularized by Robert C. Martin, for keeping code easy to change safely: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation and Dependency Inversion. Together they ask for focused classes, extension without modifying tested code, safely substitutable subclasses, small interfaces and dependence on abstractions.

4 min read

SOLID is an acronym for five object-oriented design principles, popularized by Robert C. Martin, that describe how to structure classes so a codebase stays flexible and easy to change as it grows, instead of becoming increasingly fragile with every new feature.

The five principles

S: Single Responsibility Principle. A class should have exactly one reason to change, meaning it should do one job well, not several unrelated ones bundled together.

O: Open/Closed Principle. Classes should be open for extension but closed for modification, you should be able to add new behavior without editing existing, already-tested code.

L: Liskov Substitution Principle. A subclass should be usable anywhere its superclass is expected, without breaking the program's correctness. If substituting a subclass in changes expected behavior, the inheritance relationship is wrong.

I: Interface Segregation Principle. Classes shouldn't be forced to implement interface methods they don't actually use, prefer several small, focused interfaces over one large, general-purpose one.

D: Dependency Inversion Principle. High-level modules shouldn't depend directly on low-level modules, both should depend on abstractions (interfaces), so implementation details can be swapped without touching the high-level logic.

A worked example: Liskov Substitution violated, then fixed

// A classic, real violation of Liskov Substitution
class Rectangle {
    protected int width, height;
    void setWidth(int w)  { width = w; }
    void setHeight(int h) { height = h; }
    int area() { return width * height; }
}

class Square extends Rectangle {
    @Override
    void setWidth(int w)  { width = w; height = w; }   // forces both sides equal
    @Override
    void setHeight(int h) { width = h; height = h; }
}

void testArea(Rectangle r) {
    r.setWidth(5);
    r.setHeight(4);
    System.out.println(r.area());   // expects 20 for any Rectangle
}
// testArea(new Square(...)) actually prints 16, silently breaking the caller's assumption

Square technically compiles as a Rectangle, but substituting it in breaks the caller's reasonable expectation that setting width and height independently gives an area of width times height, a genuine Liskov Substitution violation. The real fix is to not model Square as a subclass of Rectangle at all, since a square isn't safely substitutable for a general rectangle, both should instead implement a shared Shape interface with their own independent area() logic.

Where this matters in real systems

Dependency Inversion is the principle behind essentially every modern backend framework's dependency injection system (Spring in Java, .NET's built-in DI container): high-level business logic depends on an interface like PaymentGateway, not on a concrete StripeGateway class directly, so the concrete implementation can be swapped (for testing, or for switching providers) without touching a single line of the business logic. Interface Segregation shows up constantly in well-designed APIs: instead of one giant Worker interface with work(), eat(), and sleep(), you'd have separate Workable, Eatable, and Sleepable interfaces, so a Robot class only implements Workable instead of being forced to also implement irrelevant eat()/sleep() methods.

What interviewers actually ask about SOLID

  • "Explain the Single Responsibility Principle with an example." A class that handles both saving user data to a database and formatting it for an email report has two reasons to change (a database schema change, or an email format change), violating SRP, it should be split into two focused classes.
  • "What does Open/Closed actually mean in practice?" Instead of editing an existing class's if/else chain every time a new case is added, design it so new behavior can be added via a new subclass or implementation, without modifying the original, already-tested code.
  • "Give a real, common violation of Liskov Substitution." The Square-extends-Rectangle example above is the textbook case, taught precisely because it looks reasonable at first glance but breaks real caller assumptions.
  • "Why is Dependency Inversion useful for testing?" Depending on an interface instead of a concrete class lets you substitute a fake/mock implementation during tests, without needing a real database, real network call, or real external service.

Key takeaways

  • SOLID is five principles: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion.
  • Single Responsibility: one class, one reason to change.
  • Open/Closed: extend behavior without modifying existing, tested code.
  • Liskov Substitution: a subclass must be safely usable anywhere its superclass is expected.
  • Dependency Inversion underlies how real dependency injection frameworks let implementations be swapped freely.
All 72 OOP interview questions

More in Misc

Log in to save which topics you've read across every subject.

Frequently asked questions

Explain the Single Responsibility Principle with an example.

A class that handles both saving user data to a database and formatting it for an email report has two reasons to change (a database schema change, or an email format change), violating SRP, it should be split into two focused classes.

What does Open/Closed actually mean in practice?

Instead of editing an existing class's `if/else` chain every time a new case is added, design it so new behavior can be added via a new subclass or implementation, without modifying the original, already-tested code.

Give a real, common violation of Liskov Substitution.

The Square-extends-Rectangle example above is the textbook case, taught precisely because it looks reasonable at first glance but breaks real caller assumptions.

Why is Dependency Inversion useful for testing?

Depending on an interface instead of a concrete class lets you substitute a fake/mock implementation during tests, without needing a real database, real network call, or real external service.