SOLID & Design Patterns Interview Question #7

What are common signs of an LSP violation?

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

Quick Interview Answer

A subtype that throws NotSupportedException for an operation promised by the base abstraction is a common warning sign. Another is when callers must check the concrete subtype before safely using a base reference.

Detailed Explanation

A subtype that throws NotSupportedException for an operation promised by the base abstraction is a common warning sign. Another is when callers must check the concrete subtype before safely using a base reference.

These symptoms often indicate that the inheritance hierarchy models the domain incorrectly and should be redesigned using smaller abstractions or composition.

Code Example

public abstract class Bird
{
    public abstract void Fly();
}

public class Penguin : Bird
{
    public override void Fly()
        => throw new NotSupportedException();
}