Liskov Substitution principle
The Liskov Substitution Principle (LSP) is one of the SOLID principles of object-oriented design. It is named after Barbara Liskov, who introduced the concept in 1987. The principle ensures that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.
Key Concept:
"Subtypes must be substitutable for their base types." This means that an object of a derived class should be able to replace an object of the base class without altering the desirable properties of the program (e.g., correctness, task performed).
Why It's Important:
LSP helps to ensure that a class can be extended without introducing errors or unexpected behaviors, promoting code reusability and maintainability.
Example:
Consider a base class Bird and derived classes Sparrow and Penguin. According to LSP, any instance of Penguin should behave like an instance of Bird when used interchangeably.
Without Violating LSP:
csharp
public class Bird
{
public virtual void Fly()
{
Console.WriteLine("Bird is flying");
}
}
public class Sparrow : Bird
{
public override void Fly()
{
Console.WriteLine("Sparrow is flying");
}
}
public class Penguin : Bird
{
public override void Fly()
{
throw new NotImplementedException();
}
}
public void MakeBirdFly(Bird bird)
{
bird.Fly();
}
In this example, the Penguin class cannot fly, which violates LSP because calling Fly on a Penguin instance will throw an exception, disrupting the program's expected behavior.
Following LSP:
csharp
public abstract class Bird
{
// Common bird properties and methods
}
public interface IFlyingBird
{
void Fly();
}
public class Sparrow : Bird, IFlyingBird
{
public void Fly()
{
Console.WriteLine("Sparrow is flying");
}
}
public class Penguin : Bird
{
// Penguin-specific properties and methods
}
public void MakeBirdFly(IFlyingBird flyingBird)
{
flyingBird.Fly();
}
In this improved design, we separate the flying capability into an interface IFlyingBird, so only flying birds like Sparrow implement it. This way, Penguin remains a bird without inheriting the Fly method, adhering to LSP.
Benefits of LSP:
Enhances Code Reusability: Ensures that new derived classes can be used interchangeably with base classes.
Improves Maintainability: Helps to avoid unexpected behaviors and errors when extending classes.
Promotes Robustness: Guarantees that changes in derived classes do not break the functionality expected by base classes.
By following the Liskov Substitution Principle, you can create more robust, maintainable, and flexible code. If you have any more questions or need further examples, feel free to ask!