What are the SOLID principles?

SOLID is a set of five object-oriented design principles that help developers create software that is maintainable, scalable, flexible, and easy to understand. These principles encourage loose coupling, high cohesion, and clean architecture, making applications easier to extend and modify over time.

Key Points

S - Single Responsibility Principle (SRP)

A class should have only one responsibility and therefore only one reason to change.

Bad Example

An Employee class that

This class has multiple responsibilities.

Good Example

Benefit

O - Open/Closed Principle (OCP)

Software entities should be open for extension but closed for modification.

Instead of modifying existing code, new functionality should be added through extension.

Example

Payment System

Instead of modifying existing code every time a new payment method is added:

Create new implementations such as

Benefit

L - Liskov Substitution Principle (LSP)

A subclass should be able to replace its parent class without changing the correctness of the program.

Example

If a method accepts a Bird object

Bird bird = new Sparrow();

It should work correctly when any valid Bird subclass is passed.

Violation Example

If Penguin extends Bird but cannot perform behaviors expected from Bird, the design may violate LSP.

Benefit

I - Interface Segregation Principle (ISP)

Clients should not be forced to depend on methods they do not use.

Bad Example

interface Worker {

    void work();

    void eat();
}

A Robot class implementing this interface would be forced to implement eat() unnecessarily.

Good Example

interface Workable {

    void work();
}

interface Eatable {

    void eat();
}

Benefit

D - Dependency Inversion Principle (DIP)

High-level modules should depend on abstractions rather than concrete implementations.

Bad Example

class NotificationService {

    EmailService emailService =
            new EmailService();
}

The service is tightly coupled to EmailService.

Good Example

interface MessageService {

    void sendMessage();
}

class NotificationService {

    private MessageService messageService;

    NotificationService(
            MessageService messageService) {

        this.messageService =
                messageService;
    }
}

Benefit

Example

Consider an e-commerce application.

SRP

OCP

LSP

ISP

DIP

interface Payment {

    void pay();
}

class UpiPayment
        implements Payment {

    @Override
    public void pay() {

        System.out.println(
                "Payment through UPI");
    }
}

class PaymentService {

    private Payment payment;

    public PaymentService(
            Payment payment) {

        this.payment = payment;
    }

    public void processPayment() {

        payment.pay();
    }
}

public class Demo {

    public static void main(String[] args) {

        Payment payment =
                new UpiPayment();

        PaymentService service =
                new PaymentService(payment);

        service.processPayment();
    }
}

Output

Payment through UPI

Benefits of SOLID Principles

Interview Tip

A concise interview answer is

SOLID is a collection of five object-oriented design principles: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. These principles help developers build maintainable, scalable, loosely coupled, and extensible applications by promoting clean design and proper separation of responsibilities.

Last updated: 11 November 2026

Previous question: Can we use objects as keys in a TreeMap?

Next question: What is a thread in Java, and how do you create one?

More Core Java - Level II interview questions

All Core Java - Level II questions