SOLID Principles in Java
Understanding SOLID Principles with
Examples in Java
What is SOLID?
• SOLID is an acronym for five design principles
intended to make software designs more
understandable, flexible, and maintainable.
• 1. **S**ingle Responsibility Principle (SRP)
• 2. **O**pen/Closed Principle (OCP)
• 3. **L**iskov Substitution Principle (LSP)
• 4. **I**nterface Segregation Principle (ISP)
• 5. **D**ependency Inversion Principle (DIP)
Single Responsibility Principle (SRP)
• A class should have only one reason to
change, meaning it should only have one
responsibility.
• **Example in Java:**
• ```java
• class Book {
• private String content;
• public String getContent() { return content; }
Open/Closed Principle (OCP)
• Software entities should be open for extension
but closed for modification.
• **Example in Java:**
• ```java
• abstract class Shape {
• abstract double area();
• }
Liskov Substitution Principle (LSP)
• Objects of a superclass should be replaceable
with objects of a subclass without affecting
the correctness of the program.
• **Example in Java:**
• ```java
• class Bird {
• void fly() {}
• }
Interface Segregation Principle
(ISP)
• Clients should not be forced to implement
interfaces they do not use.
• **Example in Java:**
• ```java
• interface Workable {
• void work();
• }
Dependency Inversion Principle
(DIP)
• High-level modules should not depend on low-
level modules. Both should depend on
abstractions.
• **Example in Java:**
• ```java
• interface Keyboard {}
• class WiredKeyboard implements Keyboard {}