0% found this document useful (0 votes)
5 views6 pages

SOLID Principles in Java___

The document outlines the SOLID principles in Java, providing definitions and examples for each principle. It includes the Single Responsibility Principle, Open/Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle, contrasting bad and good coding practices. These principles aim to improve software design and maintainability by promoting better coding practices.

Uploaded by

Ramesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

SOLID Principles in Java___

The document outlines the SOLID principles in Java, providing definitions and examples for each principle. It includes the Single Responsibility Principle, Open/Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle, contrasting bad and good coding practices. These principles aim to improve software design and maintainability by promoting better coding practices.

Uploaded by

Ramesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SOLID Principles in Java with Real-Time Examples

1. Single Responsibility Principle (SRP)

Definition: A class should have only one reason to change.

Bad Example:

public class Employee {

public String getEmployeeDetails() {

return "Employee Data";

public void generatePDFReport() {

// Generates report

Good Example:

public class Employee {

public String getEmployeeDetails() {

return "Employee Data";

public class ReportGenerator {

public void generatePDFReport(Employee emp) {

// Generates report

}
SOLID Principles in Java with Real-Time Examples

2. Open/Closed Principle (OCP)

Definition: Software entities should be open for extension, but closed for modification.

Bad Example:

public class DiscountCalculator {

public double calculate(String customerType) {

if (customerType.equals("Premium")) return 0.2;

else if (customerType.equals("Regular")) return 0.1;

return 0;

Good Example:

public interface DiscountStrategy {

double getDiscount();

public class PremiumCustomer implements DiscountStrategy {

public double getDiscount() { return 0.2; }

public class RegularCustomer implements DiscountStrategy {

public double getDiscount() { return 0.1; }

public class DiscountCalculator {

public double calculate(DiscountStrategy strategy) {

return strategy.getDiscount();
SOLID Principles in Java with Real-Time Examples

3. Liskov Substitution Principle (LSP)

Definition: Subtypes must be substitutable for their base types.

Bad Example:

public class Bird {

public void fly() {

System.out.println("Flying");

public class Ostrich extends Bird {

public void fly() {

throw new UnsupportedOperationException("Ostrich can't fly");

Good Example:

public interface Bird {}

public interface FlyingBird extends Bird {

void fly();

public class Sparrow implements FlyingBird {


SOLID Principles in Java with Real-Time Examples

public void fly() {

System.out.println("Flying");

public class Ostrich implements Bird {

// No fly method

4. Interface Segregation Principle (ISP)

Definition: No client should be forced to depend on interfaces it does not use.

Bad Example:

public interface Worker {

void work();

void eat();

Good Example:

public interface Workable {

void work();

public interface Eatable {

void eat();

}
SOLID Principles in Java with Real-Time Examples

public class Robot implements Workable {

public void work() {

System.out.println("Working");

5. Dependency Inversion Principle (DIP)

Definition: High-level modules should not depend on low-level modules; both should depend on abstractions.

Bad Example:

public class MySQLDatabase {

public void connect() {

System.out.println("Connected to MySQL");

public class App {

private MySQLDatabase db = new MySQLDatabase();

public void start() {

db.connect();

Good Example:

public interface Database {


SOLID Principles in Java with Real-Time Examples

void connect();

public class MySQLDatabase implements Database {

public void connect() {

System.out.println("Connected to MySQL");

public class App {

private Database db;

public App(Database db) {

this.db = db;

public void start() {

db.connect();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy