Solid principles in java

Serxan Hamzayev
JavaToDev
Published in
6 min readJan 9, 2023

--

There are a number of SOLID principles that are often followed when designing and developing software using the Java programming language. These principles are:

1. Single Responsibility Principle

The Single Responsibility Principle (SRP) is a principle of software design that states that a class or module should have only one reason to change. This means that a class or module should have only one responsibility or job, and all of its methods and variables should be related to that responsibility.

For example, consider a class that represents a car. A car has many responsibilities, such as driving, braking, and honking the horn. If we try to put all of these responsibilities into a single class, it might look like this:

class Car {

private int speed;

public void drive() {
// code to make the car drive
}

public void brake() {
// code to make the car brake
}

public void honkHorn() {
// code to make the car honk its horn
}
}

This class violates the Single Responsibility Principle, because it has multiple responsibilities (driving, braking, and honking the horn). A better way to design this class would be to split the responsibilities into separate classes:

class Car {

private int speed;

public void drive() {
// code…

--

--