0% found this document useful (0 votes)
4 views

SOLID_Principles_in_Java

Uploaded by

Gabriela Beda
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SOLID_Principles_in_Java

Uploaded by

Gabriela Beda
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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 {}

You might also like