Solid Principle in Java
Solid Principle in Java
1) Single Responsibility Principle (SRP) :- every Java class must perform a single functionality ex.
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.
5) Dependency Inversion Principle (DIP) :- The principle states that we must use abstraction (abstract cla
sses and interfaces) instead of concrete implementations.