---
title: "SOLID Principle"  
description: "The SOLID principles are a set of five design principles in object-oriented programming. They help developers create robust, maintainable, and scalabl"  
author: "Ravi Vishwakarma"  
published: 2025-01-08  
updated: 2025-01-16  
canonical: https://www.mindstick.com/articles/338105/solid-principle  
category: "software development"  
tags: ["c#", "java", "oops"]  
reading_time: 3 minutes  

---

# SOLID Principle

**The SOLID principles are a set of five design [principles in object-oriented](https://www.mindstick.com/interview/34041/understanding-solid-principles-in-object-oriented-design) programming.**

![SOLID Principle](https://www.mindstick.com/mindstickarticle/5621bb5f-3326-4b17-ac7b-418d93197969/images/6e1b7545-febf-4020-871d-1312debba455.png)

#### 1. Single Responsibility Principle (SRP)

**Definition:** A class should have only [one reason](https://answers.mindstick.com/qa/42672/what-was-one-reason-for-the-failure-of-british-troops-in-the-south) to change, meaning it should have only one [responsibility](https://answers.mindstick.com/qa/104498/what-is-the-significance-of-the-fiscal-responsibility-and-budget-management-act).\
**Purpose:** Avoids tightly coupling unrelated functionalities, making code easier to understand and maintain.\
**Example:**\
Instead of having the `ReportManager` class that both generates and sends reports, separate it into:

- `ReportGenerator` for generating reports.
- `ReportSender` for sending them.

#### 2. Open/Closed Principle (OCP)

**Definition:** Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.\
**Purpose:** Encourages writing code that can be extended without altering existing code, minimizing the risk of introducing bugs.\
**Example:**\
Use [inheritance](https://www.mindstick.com/articles/1810/objective-c-inheritance) or [interfaces](https://www.mindstick.com/forum/1033/issue-with-generics-interfaces-and-casting) to extend [functionality](https://www.mindstick.com/interview/547/explain-about-the-functionality-of-vb-script) rather than modify existing classes.

```cs
public interface IShape { double CalculateArea(); } public class Circle : IShape { public double Radius { get; set; } public double CalculateArea() => Math.PI * Radius * Radius; } public class Rectangle : IShape { public double Width { get; set; } public double Height { get; set; } public double CalculateArea() => Width * Height; }
```

#### 3. Liskov Substitution Principle (LSP)

**Definition:** Objects of a superclass should be replaceable with objects of its subclass without altering the correctness of the program.\
**Purpose:** Ensures a subclass can stand for its superclass without unexpected behavior.\
**Example:**\
If `Bird` has a method `Fly()`, a subclass like `Penguin` that can't fly violates LSP.\
To fix this, refactor the hierarchy:

```cs
public interface IBird { void Eat(); } public interface IFlyingBird : IBird { void Fly(); } public class Sparrow : IFlyingBird { public void Eat() { /* Implementation */ } public void Fly() { /* Implementation */ } } public class Penguin : IBird { public void Eat() { /* Implementation */ } }
```

#### 4. Interface Segregation Principle (ISP)

**Definition:** A class should not be forced to implement interfaces it does not use.\
**Purpose:** Avoids creating large interfaces; promotes small, specific ones.\
**Example:**

Instead of a single large interface:

```cs
public interface IVehicle { void Drive(); void Fly(); }
```

Create smaller interfaces:

```cs
public interface IDrivable { void Drive(); } public interface IFlyable { void Fly(); } public class Car : IDrivable { public void Drive() { /* Implementation */ } } public class Airplane : IFlyable { public void Fly() { /* Implementation */ } }
```

#### 5. Dependency Inversion Principle (DIP)

**Definition:** High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.\
**Purpose:** Makes systems flexible and reduces coupling between components.\
**Example:**\
Instead of directly instantiating dependencies:

```cs
public class DataAccess { public void GetData() { /* Implementation */ } } public class BusinessLogic { private DataAccess _dataAccess = new DataAccess(); }
```

Use [dependency injection](https://www.mindstick.com/interview/1109/what-are-the-different-types-of-dependency-injection-explain-with-examples) with interfaces:

```cs
public interface IDataAccess { void GetData(); } public class DataAccess : IDataAccess { public void GetData() { /* Implementation */ } } public class BusinessLogic { private readonly IDataAccess _dataAccess; public BusinessLogic(IDataAccess dataAccess) { _dataAccess = dataAccess; } }
```

#### Summary

The SOLID principles provide a framework for writing clean, maintainable, and scalable object-oriented code. Following these principles reduces coupling, enhances flexibility, and [makes your](https://www.mindstick.com/blog/11381/what-makes-your-business-better) codebase easier to understand and modify.

---

Original Source: https://www.mindstick.com/articles/338105/solid-principle

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
