LSP : Liskov Substitution Principle

In this article, we are discussing the sub-topic of the design principle , LSP : Liskov Substitution Principle.

This is the 10th article on the System Design and Software Architecture series. In this article, we are discussing the sub-topic of the design principle, LSP : Liskov Substitution Principle.

Previous articles

What is The Liskov Substitution Principle?

At a higher level, the LSP states that in an object-oriented program. If we replace a super-class object reference with an object in any of its subclasses also the program should not break.

According to the LSP, functions that use references to basic classes can use it without knowing the objects of the derivative class. In simple words, derivative classes should need to substitute for the basic class. To illustrate LSP, we take the example of rectangles and squares. Someone tends to confirm the ISA connection and, thus, you can say it is a rectangle.

The Liskov Substitution Principle states: “In a computer program if S is a subtype of T, T-type objects that can be substituted for S-type objects without modification (i.e., S-type objects have the ability to substitute for T-type objects), which is one of the preferred properties of the program (That, the work performed, etc.). Simply put, in an object-oriented program, any object in a class can replace by a child class object. To better understand this principle, we would like to make a brief mention of the concept of heritage and its properties, as well as a brief reminder of a subtype of polymerization.

What is the quality of the code represented by the LSP : Liskov Substitution Principle?

The following are some of the qualitative characteristics of the codes represented by the Liskov substitution principle.

  • Only when derivatives are completely substituted for their basic types can the functions that use those basic types can be reused without unity and the derivatives that can be modified without unity.
  • LSP also refer as “contract planning”. Using this scheme, the class system states the preconditions and post-conditions. The preconditions for the system to operate must true. Upon completion, the system certifies that the post-condition is true.
  • Contract design affects the “throw” exception statement as well as the runtime exception throw and general try/catch.
  • Inheritance is a fairly simple concept that one can understand. It occurs when an object or class is based on another object or class. When a class is “inherited” from another class, it means that the inherited class (also known as the subclass or children’s class) contains all the characteristics of the superior class (mother class) but may also contain new attributes. We illustrate this with a common example: if you have a class watch, you can inherit it from that class to get a class pocket watch. The pocket watch is still a watch; it has a few extra features only. Another example is the class where the mother is the woman with the child class. In addition to having a baby but the point is a mother is still a woman.
  • This brings us to the next term that needs to be explained, which is called a millimeter: objects that can be manipulated in one way and manipulated in another. In object-oriented programming, this is called context-dependent behavior. To use the last example: A mother behaves like a mother when she walks with her child or attends a school parent meeting. But when she is with her friends, at work, or when she makes a mistake, she will behave like a woman.
  • The subtype is a concept that is not similar to a millimeter concept. However, the two are closely intertwined in common languages ​​such as C++, Java, and C #, and there is practically no difference between them. Even for completeness, we provide a formal definition of sub classification. But the details are not discussed in this article. “In programming language theory, a subtype (subclass multimeter or insertion multimeter) is a taxonomic polymer. Also, a subtype of which is a database that is linked to another database (supertype) and it is based on some concept of substitution, that is, the program element. Subtitles or functions written to operate on the elements of the supertype can also operate on the element of the subtype. If S is a subtype of T, the subtype relation is often written as S <: T, which means that any S type application can use very safely in a context where a T type term is desired. “

Requirements

  • Liskov substitution principle in new object-oriented programming languages ​​(usually at the class level rather than types; nominal versus standard).
  • The contradiction of method parameters of subtype.
  • Correlation of subtype method returns types.
  • New exceptions cannot discard by subtype methods and except for subtypes of exceptions thrown by supertype methods.
  • In addition to signature requirements, also the subtype must meet several behavioral conditions. These are described in detail in the terminology similar to the plan in terms of contract methodology, which may impose some limitations on how the contract inheritance can interact with:
  • The preconditions of the subtype cannot be strengthened.
  • Subsequent conditions of the subtype should not weaken.
  • Changes must need to preserve within the subcategory.

History boundary (“History rule”).

The history barrier forbids this. It is a new feature introduced by Liskov and Wing.

  • This limit can exemplify by defining a debt by defining a distortion point as a subtype of an irreversible point. This is a violation of the boundaries of history because in the history of the irreversible point and the history of a distorted point, in general, that cannot be included, as the state after creation is always the same.
  • However, the fields added to the subcategory can safely modify because they have no ability to track by super-type methods. Thus, a circle with an irreversible center and a distorted radius can define as a sub-variant of a point that has no ability to change without indebtedness. Also without violating the boundaries of history.

Conclusion

Thanks for reading the article LSP : Liskov Substitution Principle as an essential component in System design and architecture.

Originally published at https://onloadcode.com on March 28, 2021.

I’m passionate about continuously learning and exploring new technologies, which will enhance my expertise status. Currently, I'm working as a Technical Lead.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store