Sitemap
Abhishek Liyanage

Abhishek Liyanage

SOLID Principles in Java — Single Responsibility(Part 1)

2 min readAug 24, 2026
Press enter or click to view image in full size

SOLID is a set of five design principles that can help us write cleaner, more maintainable and flexible object-oriented code. In this series, I’ll explain each principle using simple Java examples.

S — Single Responsibility
O — Open/Closed Principle
L — Liskov Substitution Principle
I — Interface Segregation Principle
D — Dependency Inversion Principle

Single Responsibility — A class should have a single responsibility

See the code segment below:

public class EmployeeService {

public void addEmployee(Employee employee) {
// save employee method
}

public void generateSalarySlip(Employee employee) {
// calculate salary method
}

public void sendEmail(Employee employee) {
// send email method
}
}

This EmployeeService class is doing 3 different jobs.
* Employee Management
* Salary Calculation
* Email Communication

Imagine you have to change the email system, then you have to change the EmployeeService class. If salary calculation changes, you again have to change the EmployeeService class.

Get Abhishek Liyanage’s stories in your inbox

Join Medium for free to get updates from this writer.

A better design is to separate these responsibilities through separate classes like below

public class EmployeeService {

public void addEmployee(Employee employee) {
// save employee
}
}
public class SalaryService {

public void generateSalarySlip(Employee employee) {
// calculate salary
}
}
public class EmailService {

public void sendEmail(Employee employee) {
// send email
}
}

Now you can see each class has a much cleaner responsibility.

EmployeeService class → Employee Management
SalaryService class → Salary Management
EmailService class → Email Management

Also note that it does not mean a class can only have one method. We can add more methods based on the class's responsibility.

public class EmployeeService {

public void addEmployee(Employee employee) {
// save employee
}

public void updateEmployee(Employee employee) {
// update employee
}

public void deleteEmployee(Employee employee) {
// remove employee
}

}

The goal of Single Responsibility is not to create as many classes as possible. Instead, it is about keeping responsibilities focused so that changes in one area don’t unnecessarily affect unrelated parts of the system.

I will discuss the second principle in the next article.

Thank you & Good Luck!
Abhishek Liyanage

--

--