Exam Notes

icon picker
Week 10: Liskov Substitution Principle and SOLID Principles in C#

1. Overview of SOLID Principles

The SOLID principles are foundational for writing clean, maintainable, and scalable object-oriented code. Each principle supports specific aspects of object-oriented design.
Single Responsibility Principle (SRP): A class should only have one reason to change, meaning it should have only one responsibility.
Open/Closed Principle (OCP): Classes should be open for extension but closed for modification.
Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types without altering the correctness of the program.
Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use.
Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules; instead, both should depend on abstractions.

2. Liskov Substitution Principle (LSP)

Definition: LSP states that objects of a derived class should be able to replace objects of the base class without changing the correctness or expected behavior of the program.
Example in C#:
If class B inherits from class A, B should be able to be used in any context where A is expected without introducing errors or unexpected behavior.
Importance of LSP:
Ensures code reliability and polymorphic behavior.
Maintains substitutability, which is essential for writing reusable and maintainable code.

Example of LSP Violation

If a Rectangle class is inherited by a Square class, setting width or height independently in Square violates LSP because a Square inherently requires width and height to be the same. This could lead to unexpected behavior if Square is used in place of Rectangle.

3. Interface Segregation Principle (ISP)

Definition: ISP states that classes should not be forced to implement interfaces they do not use.
Application:
Prefer multiple specific interfaces over a large, general-purpose one. This avoids forcing classes to implement unnecessary methods.

Example:

Rather than a broad IMachine interface with methods like Print, Scan, and Fax, use separate interfaces like IPrinter, IScanner, and IFax so classes can implement only the methods they need.

4. Dependency Inversion Principle (DIP)

Definition: High-level modules should depend on abstractions, not on low-level modules. Both should depend on interfaces or abstract classes.
Purpose:
Promotes loose coupling by reducing dependencies between high-level and low-level components.

Example in C#:

Use dependency injection to pass an ILogger interface instead of a concrete FileLogger to a class that requires logging. This way, you can change logging behavior by passing a different implementation without modifying the dependent class.

5. Quick Tips for MCQ Preparation

Understand Key Definitions: Know the primary goal of each SOLID principle and how they apply to object-oriented design.
Identify Examples and Violations: Be able to recognize situations that violate principles like LSP or ISP.
Practice with Scenarios: Review examples that show both correct and incorrect applications of SOLID principles, as these are common exam questions.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.