Liskov Substitution Principle

LSP

  • A program that uses an interface must not be confused by an implementation of that interface
  • A subclass should require nothing more and promise nothing less
  • LSP is a principle of extension and definition of method.
  • LSP is a principle of Polymorphism supporting OCP
  • Violated LSP can not support OCP
if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program. When replaced with object of type S, Don't use downcast, instanceof for specific type. For example There is a Vehicle Class is a super type. Police car, truck and  taxi can be a subtype of Vehicle, but Helicopter can not.
It means even if police car should be replaced by taxi or truck, The program should work well. It's similar to OCP and shows Polymorphism of OOP

Comments