12/19/2023 0 Comments Liskov substitution![]() ![]() First, we need a generic base Interface, i.e., IFruit, which will be the base class for both Apple and Orange. Let’s modify the previous example to follow the Liskov Substitution Principle using C# Language. Example Using the Liskov Substitution Principle in C# Let us see how to overcome the design issue and make the application follow the Liskov Substitution Principle using C# Langauge. That means there is some issue with our software design. So, in this case, if we are getting the color Apple instead of Orange, it follows the Liskov Substitution Principle. The Liskov Substitution Principle states that even if the child object is replaced with the parent, the behavior should not be changed. That means the behavior changes once the child object is replaced, i.e., Apple stores the Orange object. So, we can store the child class object in the Parent class Reference variable, i.e., Apple apple = new Orange() and when we call the GetColor, i.e., apple.GetColor(), then we are getting the color Orange, not the color of an Apple. using System Īs you can see in the above example, Apple is the base class, and Orange is the child class, i.e., there is a Parent-Child relationship. The point is that an Orange cannot be replaced by an Apple, which results in printing the color of the apple as Orange, as shown in the example below. Then, we create the Orange class, which inherits the Apple class and overrides the GetColor method of the Apple class. In the following example, first, we create the Apple class with the method GetColor. We will see the problem if we are not following the Liskov Substitution Principle, and then we will see how we can overcome such problems using the Liskov Substitution Principle. Let us first understand one example without using the Liskov Substitution Principle in C#. Example Without using the Liskov Substitution Principle in C#: So here, in this case, the son can’t simply replace his father even though both belong to the same family. If you are not getting this point properly, don’t worry we will see some real-time examples to understand this concept.įor example, a father is a teacher, whereas his son is a doctor. In simple words, we can say that when we have Parent-Child relationships, i.e., Inheritance Relationships between two classes, then if we successfully replace the object/instance of a parent class with an object/instance of the child class without affecting the behavior of the base class instance, it is said to be in Liskov Substitution Principle. That means child class objects should be able to replace parent class objects without compromising application integrity. So, the Liskov Substitution Principle says that the object of a derived class should be able to replace an object of the base class without bringing any errors in the system or modifying the behavior of the base class. This principle states that if S is a subtype of T, then objects of type T should be replaced with objects of type S. The Liskov Substitution Principle is a Substitutability principle in object-oriented programming Language. ![]() How to Use the Liskov Substitution Principle in C#? What is the Liskov Substitution Principle in C#?. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |