Topics
Published on January 11, 2018 by kudvenkat

Text version of the video
csharp-video-tutorials.blogspot.com/2018/01/liskov-substitution-principle.html

Slides
csharp-video-tutorials.blogspot.com/2018/01/liskov-substitution-principle-slides.html

SOLID Design Principles Tutorial

All Dot Net and SQL Server Tutorials in English
www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
www.youtube.com/c/KudvenkatArabic/playlists

In this video we will learn

1. Liskov Substitution Principle

2. Implementation guidelines of Liskov Substitution Principle

3. And will implement this Principle with a simple example

In the first video of SOLID Introduction we have understood that L in the SOLID is acronym for Liskov Substitution Principle which is also known as LSP.

Definition : Substitutability is a principle in object-oriented programming and it states that, in a computer program, if S is a Subtype of T, then objects of type T may be replaced with objects of type S

1. Which means, Derived types must be completely substitutable for their base types

2. More formally, the Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called (strong) behavioral subtyping

3. This Principle is introduced by Barbara Liskov in 1987 during her conference address on Data abstraction and hierarchy

4. This principle is just an extension of the Open Close Principle

The examples used in this session are related to the open closed principle. Hence we request you to watch the Open Closed Principle tutorial before proceeding.

Implementation guidelines : In the process of development we should ensure that

1. No new exceptions can be thrown by the subtype unless they are part of the existing exception hierarchy.

2. We should also ensure that Clients should not know which specific subtype they are calling, nor should they need to know that. The client should behave the same regardless of the subtype instance that it is given.

3. And last but not the least, New derived classes just extend without replacing the functionality of old classes

In the previous session as part of the Open closed Principle implementation we have created different employee classes to calculate bonus of the employee. From the employee perspective we have implemented the Open closed principle.

Now if you take a look at the main program, we have created Employee objects which consists of both permanent and contract employee.

If you take a closer look at this program the Derived types which are Permanent and TemporaryEmployee have completely substituted the base type employee class.

So, based on the Liskov substitution principle we have achieved LSP by ensuring that Derived types are completely substitutable for their base types.

Also, notice the main program, without using the subtypes we are calculating the bonus of the employee from the base class type itself. Hence, we are satisfying the Liskov substitution principle.

That means along with the Open Closed Principle we have partially implemented the LSP.

Also, I can state that this implementation is not adhering to guide lines of Liskov principle

To understand why it’s not adhering to the Liskov Principle, Let’s assume that we need to have a Contract Employee as one of the employee category. A point to note here is a contract employee is not eligible for any bonus calculation and post implementing the Employee class we end up throwing exception at the runtime in the caclculatebonus() method. This violates the Liskov Substitution Principle.

Hence, Please follow the below code which addresses this issue. Also, we recommend to watch our video tutorials for complete guidance and understanding of the code.

The code is available on our blog at the following link.
csharp-video-tutorials.blogspot.com/2018/01/liskov-substitution-principle.html

Leave a Reply

8 Comments on "Liskov Substitution Principle"

Notify of
avatar

Ek Srikanth
Guest
Ek Srikanth
6 days 19 hours ago

What about CalculateBonus?… at the end

Nikhilesh Naregal
Guest
Nikhilesh Naregal
7 days 1 hour ago

Hello AvishWas able to understand LSP. However, I do have a question related to one of the guideline that says "Client should not know which subtype they are calling". We have created respective child class instances as needed in the main(). Can this be overcome by something called as Factory method design pattern?. Please correct me if I'm wrong, how can we achieve it.Also, when are you panning to upload the video for "Dependency Inversion Principle"?Thanks,

Prakash Singh Choudhary
Guest
Prakash Singh Choudhary
8 days 2 hours ago

I am earning 15K per month using these three links(2 lakh/year)www.itsoftpro.com/eadultfanda.blogspot.infootballtutorial325.blogspot.inFor details contact me at prakashg4u@gmail.com

Sovan Roy
Guest
Sovan Roy
8 days 13 hours ago

Thanks a lot Avish.It will be great if you show us a real time project using all the design patterns and SOLID principles in future.Thank you

Avish MSD
Guest
Avish MSD
7 days 18 hours ago

Thank you for watching the video. Really honored with your feedback and Definitely we are planning to come up with a real time project that covers SOLID and DesignPatterns

Md.shuaib siddiqui
Guest
Md.shuaib siddiqui
8 days 16 hours ago

Sir I request to plzz Upload A Demo Project On Asp.Net Mvc …Some Think Like e -Commerce Website

ashok kumar
Guest
ashok kumar
8 days 21 hours ago

please add Azure

Avish MSD
Guest
Avish MSD
7 days 18 hours ago

Thank you for watching the video. We will try our best to accommodate your suggestion

wpDiscuz