Junior — Middle
What does the letter L in the SOLID principles signify and how does it influence software system design?
sobes.tech AI
Answer from AI
The letter L in SOLID principles stands for Liskov Substitution Principle.
The essence of the principle: subclass objects should be interchangeable with superclass objects without violating the correctness of the program. That is, if class B inherits from class A, then anywhere A is used, B can be substituted without changing the expected behavior.
Impact on design:
- Ensures proper inheritance and polymorphism.
- Helps avoid unexpected errors when extending functionality.
- Promotes the creation of more reliable and maintainable systems.
Example of LSP violation:
class Bird {
public virtual void Fly() { /*...*/ }
}
class Ostrich : Bird {
public override void Fly() {
throw new NotSupportedException(); // ostrich cannot fly
}
}
Here, Ostrich violates LSP because it cannot replace Bird without changing behavior.
A proper approach is to reconsider the hierarchy or use interfaces to avoid such situations.