SOLID & Design Patterns Interview Question #6

What is the Liskov Substitution Principle (LSP)?

SOLID Principles, Design Fundamentals, Repository, Factory, Strategy & Decorator Junior Beginner

Quick Interview Answer

The Liskov Substitution Principle says objects of a derived type should be usable wherever the base type is expected without breaking the correctness or reasonable expectations of the program.

Detailed Explanation

The Liskov Substitution Principle says objects of a derived type should be usable wherever the base type is expected without breaking the correctness or reasonable expectations of the program.

A subtype should preserve the behavioral contract of its abstraction. It should not unexpectedly reject valid base-class operations, weaken guarantees, or introduce surprising side effects.

Code Example

public interface IShape
{
    double Area();
}

public sealed class Rectangle : IShape
{
    public double Width { get; init; }
    public double Height { get; init; }
    public double Area() => Width * Height;
}