Skip to content

Latest commit

 

History

History

LSP

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Liskov Substitution Principle

Acronym

  • S : Single Responsibility Principle (SRP).
  • O : Open closed Principle (OSP).
  • L : Liskov substitution Principle (LSP).
  • I : Interface Segregation Principle (ISP).
  • D : Dependency Inversion Principle (DIP).

Liskov Substitution Principle

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.

What

  1. Introduced by Barbara Liskov, it states that "objects in a program should be replaceable with instances of their sub-types without altering the correctness of that program".
  2. If a program module is using a base class, then the reference to the base class can be replaced with a derived class without affecting the functionality of the program module.
  3. We can also state that derived types must be substitutable for their base types.

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, newly derived classes just extend without replacing the functionality of old classes.

How not to do it

See LSP1 folder for source code.

This builds on the OCP example! So look at that first if you're not familiar with it.

How to do it

See LSPsolution folder for source code.