Liskov Substitution Principle (LSP) – Complete Guide
What is LSP?
The Liskov Substitution Principle (LSP) is a core concept in SOLID programming. It states that objects of a superclass should be replaceable with objects of a subclass without affecting program correctness. LSP ensures predictable behavior in inheritance hierarchies.
Why LSP is Important
- Enables safe polymorphism.
- Prevents unexpected behavior when using subclasses.
- Ensures maintainable and extendable code.
- Supports reliable inheritance hierarchies.
Stepwise Implementation of LSP
- Step 1 – Identify Base Class Contracts: Understand what behavior the base class promises.
- Step 2 – Create Subclasses Correctly: Ensure subclasses fulfill all base class contracts.
- Step 3 – Avoid Breaking Overrides: Do not override methods in a way that changes expected behavior.
- Step 4 – Test Polymorphism: Replace base class objects with subclass objects and verify correctness.
- Step 5 – Refactor Violations: Split or redesign classes to maintain substitutability.
Example: LSP in Action
Suppose we have a payment system:
- Base class: PaymentMethod
- Subclass 1: CreditCardPayment
- Subclass 2: PayPalPayment
Each subclass can replace PaymentMethod without breaking the system:
class PaymentMethod {
public void pay(double amount) {
// base payment logic
}
}
class CreditCardPayment extends PaymentMethod {
@Override
public void pay(double amount) {
// credit card payment logic
}
}
class PayPalPayment extends PaymentMethod {
@Override
public void pay(double amount) {
// PayPal payment logic
}
}
This way, any subclass can substitute the base class without breaking functionality, following LSP.
Top 10 LSP & SOLID Interview Questions
1. What is Liskov Substitution Principle (LSP)?
LSP states that objects of a superclass should be replaceable with objects of a subclass without breaking functionality.
2. Why is LSP important?
It ensures safe polymorphism, predictable behavior, and maintainable inheritance hierarchies.
3. Can you give an LSP example?
PaymentMethod is a base class; CreditCardPayment and PayPalPayment subclasses can replace it without breaking the program.
4. How to implement LSP stepwise?
Identify base class contracts, create subclasses following contracts, avoid breaking overrides, test polymorphism, refactor violations.
5. Does LSP apply only to classes?
No, it applies to interfaces, abstract classes, and any polymorphic hierarchy.
6. What are LSP benefits?
Reliable polymorphism, maintainable inheritance, fewer bugs, cleaner design, predictable behavior.
7. What happens if LSP is violated?
Replacing a base class with a subclass may break functionality and introduce unexpected behavior.
8. How to refactor code violating LSP?
Refactor the subclass to adhere to base class contracts or redesign classes to maintain substitutability.
9. Can LSP work with other SOLID principles?
Yes, LSP works with SRP, OCP, ISP, and DIP to maintain clean, modular code.
10. Example of LSP in real life?
A payment system: PaymentMethod base class with subclasses CreditCardPayment and PayPalPayment. Any can replace the base without breaking functionality.




