Using Liskov’s Substitution Principle in C#

Brandyn Reindel
3 min readMay 7, 2020
Babara Liskov (https://en.wikipedia.org/wiki/Barbara_Liskov)

Liskov’s Substitution Principal was first introduced in 1987 by Barbara Liskov, a Professor at MIT, and Ford’s School of Engineering; during her keynote conference “Data Abstraction”. She later published a paper in conjunction with Jeanette Wing where they defined the principal as follows:

Let Φ(x) be a property provable about objects x of type T. Then Φ(y) should be true for objects y of type S where S is a subtype of T.

Confusing right? Well let’s explore what this means.

Liskov’s Substitution Principal defines that objects in Object Oriented Programming of a superclass can be replaceable with objects of its own subclasses without breaking an application. In order for this to be done objects of the subclasses have to behave in the same way as the objects of their superclass. So as long as an overridden method of a subclass accepts the same input parameter values as the method of its superclass, you can achieve this. This allows you to implement less restrictive validation rules, but you can not enforce stricter ones later on in your subclasses.

Now let’s add some more context to this principal by taking a look at the following example. Assume that we have created a class Square that inherits from Rectangle but we try to assign a Square’s width and height directly.

What goes wrong?

Well, using a getter and setter for the dimensions of a rectangle, we set the height and the width to different values. However, using this same code with aSquare does not work because the sides of a square must have the same value.

A square subclass can inherit from a rectangle superclass, but since the same arguments result in different behaviors when we reuse the method on both classes, it violates Liskov’s Substitution Principle. For our code to comply with Liskov’s Substitution Principal the behavior for both classes would have to appear the same no matter what method we used.

The easiest way to correct our code and make it work with Liskov’s Substitution principal would be to change our classes hierarchy. So if we had both theRectangle and Square classes derive from a class that does not enforce any rules regarding height or width, we would be compliant with Liskov’s Substitution Principal.

Alternatively, we could update our class Square by overriding the property setters. If one dimension (height or width depending on how you write the code) is set the other dimension would be updated to match that value.

It would look something like this:

In conclusion, although the syntax of Liskov’s Substitution Principal that was published:

Let Φ(x) be a property provable about objects x of type T. Then Φ(y) should be true for objects y of type S where S is a subtype of T.

can look confusing and foreign to many, the overall idea is far simpler, and I hope this article has made it a bit more digestible for many of you. Because even though it has been over 20 years since Barbara first introduced this concept, it is still used today.

Sources:

--

--