chartvilla.blogg.se

Liskov substitution principle
Liskov substitution principle









liskov substitution principle

We will still give a formal definition of subtyping though for the sake of completeness. However, the two are so tightly connected and fused together in common languages like C++, Java and C#, that the difference between them is practically non-existent. Subtyping is a concept that is not identical to polymorphism. (As you can see, this difference is not that strict.) But when she is out with her friends, at work, or simply doing errands, she will behave as a woman. To use the last example: a mother, when taking a walk with her child or attending a school parent’s meeting, will behave as a mother. In object-oriented programming, this is called context-dependent behavior. This brings us to the next term we should explain, which is called polymorphism: objects can behave in one way in a certain situation, and in another way in some other situation. A mother is still a woman, with the addition of having a child. Another example would be a class called Woman with a child class called Mother. A pocket watch is still a watch, it just has some additional features. Let’s illustrate this with a common example: if you have a class Watch , you can inherit from that class to get a class PocketWatch. When a class is “inherited” from another class, it means that the inherited class (also called subclass, or child class) contains all the characteristics of the superclass (parent class), but can also contain new properties. It is when an object or a class are based on another object or class. Inheritance is a concept fairly simple to understand. In order to understand this principle better, we’ll make a small digression to briefly remind ourselves about the concept of inheritance and its properties, as well as subtyping, a form of polymorphism. Simply said, any object of some class in an object-oriented program can be replaced by an object of a child class. Liskov Substitution Principle states the following: “in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.)”.

#LISKOV SUBSTITUTION PRINCIPLE UPGRADE#

We have already written about the single responsibility principle, and these five principles combined are used to make object-oriented code more readable, maintainable and easier to upgrade and modify. The Liskov Substitution Principle is one of the SOLID principles of object-oriented programming ( Single responsibility, Open-closed, Liskov Substitution, Interface Segregation and Dependency Inversion).











Liskov substitution principle