Understanding Liskov Substitution Principle(LSP)

Lets Look at the below example:

Vikas Taank
3 min readMar 7, 2024

Car and biCycle are both Vehicle but , biCycle can’t substitute vehicle because biCycle does not have an engine.

LISKOV Violation

Solving above according to LISKOV Substitution Principal:

Vehicle is a generalization for Motorized and Non Motorized Vehicle.

Now Both Car and BiCycle can replace the super class and interfaces

Now Let’s Look at the Liskov Substitution Principle (LSP)

The Liskov Substitution Principle (LSP), named after Barbara Liskov, is one of the five SOLID principles of object-oriented design and programming. It formalizes a foundational concept for creating class hierarchies that are interchangeable without altering the correctness of the program.

Principle Definition

LSP states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program. In other words, if class B is a subclass of class A, then we should be…

--

--

Vikas Taank

I am new to Medium, trying to articulate my learnings so far . Please Join medium to read my articles. Please support- https://ko-fi.com/vikastaank