SOLID: Liskov Substitution Principle

Explore the Liskov Substitution Principle and how it guides the use of inheritance.

Introduction

The Liskov Substitution Principle (LSP) is one of the fundamental design principles of object-oriented design. The LSP helps guide the use of inheritance in design so that the application does not break. It states that the objects of a subclass should behave the same way as the objects of the superclass, such that they are replaceable. This rule generally applies to abstraction concepts like inheritance and polymorphism.

The Vehicle class

Let's construct a simple class called Vehicle that has some attributes and methods and a subclass Car that extends it as shown below:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.