WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Software Engineering · Intermediate · question 28 of 100

Describe the SOLID principles in software design and provide examples for each principle.?

📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

SOLID is an acronym that stands for the following five principles of object-oriented programming and design:

- Single Responsibility Principle (SRP)

- Open Closed Principle (OCP)

- Liskov Substitution Principle (LSP)

- Interface Segregation Principle (ISP)

- Dependency Inversion Principle (DIP)

In the following sections, we’ll explain each of these principles in more detail with examples.

### Single Responsibility Principle (SRP)

The Single Responsibility Principle (SRP) states that a class should have only one responsibility, meaning it should only have one reason to change. This principle is about keeping classes focused and maintainable. If a class is responsible for too many things, it becomes difficult to maintain or test.

For example, let’s take a class that manages both user input validation and database insertion logic. If we update the database schema, the class needs to be modified although it didn’t change the way data is validated. To adhere to the SRP, we should break this class into two. One with a responsibility of handling user validation and another one to persist the data into the database.

### Open Closed Principle (OCP)

The Open Closed Principle (OCP) states that a class should be open for extension but closed for modification. In simpler terms, we should design the code base in such a way that new features can be added to existing code without breaking existing functionality.

For example, let’s consider a class that deals with different types of shapes. If we plan to add new shapes like triangles in the future, we should design the class in such a way that we can add a new shape without modifying the existing class. A better solution might be to create an abstract Shape class or interface, which all other shapes inherit and get its basic properties and features.

### Liskov Substitution Principle (LSP)

The Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of any of its subclasses without causing any harm to the program. In other words, if we have a class hierarchy, we should be able to use any of its derived classes without having to change code that uses the base class.

For example, if we have a rectangle class that has two variables: width and height, and a square class that inherits from the rectangle class but always has equal width and height; objects of type Square should be able to substitute objects of type Rectangle without affecting the correctness of the system.

### Interface Segregation Principle (ISP)

The Interface Segregation Principle (ISP) states that clients should not be forced to depend on interfaces they do not use. In simpler terms, a client should not have to implement an interface it doesn’t need. We should try to break large interfaces into smaller and more specific ones.

For example, if we have an interface with a lot of methods, any class that implements that interface will have to implement all of those methods, which may lead to cluttering interfaces with many methods that are rarely used by the clients. Instead, we should try to break the interface into smaller and more specific ones, helping reduce problems caused by changes in the code base.

### Dependency Inversion Principle (DIP)

The Dependency Inversion Principle (DIP) states that high-level modules should not depend on low-level modules. Instead, they should depend on abstractions. In other words, dependencies should always be on abstractions or interfaces and not on concrete implementations.

For example, let’s suppose we have a class that depends on a logging service. Instead of directly depending on the logging service, we can create an interface or abstract class for logging and have the class depending on it. This would allow us to change the logging service without changing the dependent class.

In summary, the SOLID principles in software design provide a set of guidelines helping to make the code more maintainable, flexible, and easier to understand. Following these principles help us create a more robust and scalable codebase.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Software Engineering interview — then scores it.
📞 Practice Software Engineering — free 15 min
📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

All 100 Software Engineering questions · All topics