Solid Principle in Java

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Solid Principle in java : -

there are 5 types of SOLID principle in java

S :- Single Responsibility Principle (SRP)


O :- Open-Closed Principle (OCP)
L :- Liskov Substitution Principle (LSP)
I :- Interface Segregation Principle (ISP)
D :- Dependency Inversion Principle (DIP)

1) Single Responsibility Principle (SRP) :- every Java class must perform a single functionality ex.

public class Student


{
public void addStudent();
{
//functionality of the method
}
}

2)Open-Closed Principle (OCP) :- the module should be open for extension but closed for modification

3)Liskov Substitution Principle (LSP) :- derived classes must be completely substitutable for their base cla
sses. In other words, if class A is a subtype of class B, then we should be able to replace B with A without
interrupting the behavior of the program.

4) Interface Segregation Principle (ISP) :- The principle states that the larger interfaces split into smaller o
nes. Because the implementation classes use only the methods that are required. We should not force th
e client to use the methods that they do not want to use.

Ex .
public interface Conversion
{
public void intToDouble();
public void intToChar();
public void charToString();
}

The above interface has three methods. If we want to use only a method intToChar(), we have no choice t
o implement the single method. To overcome the problem, the principle allows us to split the interface into
three separate ones.

public interface ConvertIntToDouble


{
public void intToDouble();
}
public interface ConvertIntToChar
{
public void intToChar();
}
public interface ConvertCharToString
{
public void charToString();
}

5) Dependency Inversion Principle (DIP) :- The principle states that we must use abstraction (abstract cla
sses and interfaces) instead of concrete implementations.

You might also like