Questions tagged [liskov-substitution-principle]

For questions about the Liskov Substitution Principle (LSP) in object-oriented design, developed by Barbara Liskov and collected by Robert C. Martin as one of the SOLID principles.

Barbara Liskov presented the Liskov Substitution Principle in 1987, as a principle of object-oriented programming. It states that a program should be able to replace an instance of a type with an instance of a subtype, without affecting any desired properties of the program. LSP describes a semantic rather than merely syntactic relation, because it defines behavioral subtypes in excess of functional correctness.

What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.

For example, suppose we have a class Animal with a method walk(), and a subclass Dog inherits walk(). In the following snippet, the run-time type of x is not known.

Animal x;
x = AnimalFactory.createAnimal(some_parameter);
x.walk();

Although Dog.walk() may override Animal.walk(), and have a more specific implementation, it should not make a difference to the behavior of the program whether x is an instance of Animal or an instance of Dog.

Robert Martin included the LSP as the third of his and has equated it with .

There is a strong relationship between the LSP and the concept of Design by Contract as expounded by Bertrand Meyer. Using this scheme, methods of classes declare preconditions and postconditions. The preconditions must be true in order for the method to execute. Upon completion, the method guarantees that the postcondition will be true.

See the LSP article under Principles of OOD.

240 questions
997
votes
33 answers

What is an example of the Liskov Substitution Principle?

I have heard that the Liskov Substitution Principle (LSP) is a fundamental principle of object oriented design. What is it and what are some examples of its use?
149
votes
7 answers

Why array implements IList?

See the definition of System.Array class public abstract class Array : IList, ... Theoretically, I should be able to write this bit and be happy int[] list = new int[] {}; IList iList = (IList)list; I also should be able to call any method from…
oleksii
  • 33,544
  • 11
  • 83
  • 149
94
votes
3 answers

Can you explain Liskov Substitution Principle with a good C# example?

Can you explain Liskov Substitution Principle (The 'L' of SOLID) with a good C# example covering all aspects of the principle in a simplified way? If it is really possible.
pencilCake
  • 45,443
  • 73
  • 211
  • 346
73
votes
18 answers

Any good examples of inheriting from a concrete class?

Background: As a Java programmer, I extensively inherit (rather: implement) from interfaces, and sometimes I design abstract base classes. However, I have never really felt the need to subclass a concrete (non-abstract) class (in the cases where I…
sleske
  • 73,934
  • 32
  • 166
  • 212
73
votes
3 answers

Should constructors comply with the Liskov Substitution Principle?

I usually try to make sure my object instances comply with the Liskov Substitution Principle, but I've always wondered is do people think LSP should apply to constructors too? I've tried googling for this but I haven't been able to find any strong…
dkubb
  • 2,036
  • 18
  • 16
65
votes
8 answers

Is deriving square from rectangle a violation of Liskov's Substitution Principle?

I am new to design and learning the design principles. It says deriving square from rectangle is a classic example of violation of Liskov's Substitution Principle. If that's the case, what should be the correct design?
somaraj
  • 819
  • 1
  • 9
  • 10
64
votes
5 answers

Liskov substitution principle - no overriding/virtual methods?

My understanding of the Liskov substitution principle is that some property of the base class that is true or some implemented behaviour of the base class, should be true for the derived class as well. I guess this would mean when a method is…
54
votes
3 answers

Why doesn't Rust support trait object upcasting?

Given this code: trait Base { fn a(&self); fn b(&self); fn c(&self); fn d(&self); } trait Derived : Base { fn e(&self); fn f(&self); fn g(&self); } struct S; impl Derived for S { fn e(&self) {} fn f(&self) {} …
kFYatek
  • 4,043
  • 4
  • 18
  • 14
43
votes
8 answers

How do I avoid breaking the Liskov substitution principle with a class that implements multiple interfaces?

Given the following class: class Example implements Interface1, Interface2 { ... } When I instantiate the class using Interface1: Interface1 example = new Example(); ...then I can call only the Interface1 methods, and not the Interface2…
jml
  • 566
  • 4
  • 10
43
votes
4 answers

Do Collections.unmodifiableXXX methods violate LSP?

Liskov Substitution principle is one of the principles of SOLID. I have read this principle some number of times now and have tried to understand it. Here is what I make out of it, This principle is related to strong behavioral contract among…
Narendra Pathai
  • 38,384
  • 18
  • 73
  • 117
40
votes
5 answers

Can anyone provide an example of the Liskov Substitution Principle (LSP) using Vehicles?

The Liskov Substitution Principle states that a subtype should be substitutable for that type (without altering the correctness of the program). Can someone please provide an example of this principle in the domain of vehicles (automotives)? Can…
random512
  • 875
  • 1
  • 10
  • 17
23
votes
6 answers

ReadOnlyCollection vs Liskov - How to correctly model immutable representations of a mutable collection

Liskov-substitution principle requires that subtypes must satisfy the contracts of super-types. In my understanding, this would entail that ReadOnlyCollection violates Liskov. ICollection's contract exposes Add and Remove operations, but the…
21
votes
8 answers

Is the ReadOnlyCollection class a good example of Bad Design?

Look at the specification of the ReadOnlyCollection class, it does implements the IList interface, right. The IList interface have Add/Update/Read methods, which we call it pre-conditions of the interface. Anywhere in my application if I have an…
goenning
  • 6,214
  • 1
  • 31
  • 40
15
votes
3 answers

Does the JavaScript event system violates the LSP?

I'm asking this more out of curiosity rather than really being concerned with it, but I've been wondering whether or not the JavaScript event system violates the Liskov substitution principle (LSP) or not. By calling EventTarget.dispatchEvent, we…
plalx
  • 39,329
  • 5
  • 63
  • 83
15
votes
2 answers

How can I avoid breaking Liskov Substitution Principle (LSP)?

I am in a situation very similar to what Steve McConnell's in Code Complete has mentioned . Only that my problem is based of Vehicles and Trike happens to be on that by law falls in the category of Cars . Cars had four wheels until now . Any way my…
Java Ka Baby
  • 4,482
  • 10
  • 36
  • 50
1
2 3
15 16