Liskov Substitution

## The course is part of this learning path

Start course
Difficulty
Beginner
Duration
28m
Students
47
Ratings
5/5
Description

In this course, we explore the SOLID principles one by one and look at how they work.

### Learning Objectives

• Understand what SOLID principles are

### Intended Audience

• Beginner Java developers
• Anyone with a desire to learn about SOLID principles

### Prerequisites

• Basic Java knowledge
Transcript

Hello, dear friends. In this video, we will examine the Liskov substitution principle. So, let's begin. The Liskov substitution principle, LSP, named for and originally defined by Barbara Liskov, states that we should be able to treat a child class as though it were the parent class. Essentially, this means that all derived classes should retain the functionality of their parent class and cannot replace any functionality the parent provides. The LSP is very similar in principle to the open-closed principle. Now, let's see with an example. As you see in the image, we have a base Rectangle class, and in the second image we try to extend Square from the Rectangle. Look, with super method, we try to assign length to height or height to length. We can also extend triangle from Rectangle. In that case only, we will set to the triangle edge.

Actually, we can make this but it does not comply with the Liskov substitution principle. Therefore, we need to make a new design in accordance with the Liskov substitution principle. First, we have defined an abstract class or interface as Shape because square and rectangle are both shapes. Now we can create our Rectangle from Shape like this. As you can see, we have to override CalculateArea method. And for Square, we will add another class from Shape like this. And we have to override calculate for Square again. Now, this structure conforms to the Liskov principle. So, that's all I have to say about LSP. In the following video, we will look at the interface segregation principle. So, I'll see you there my friends.