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
14
votes
1 answer

Does Haskell's type system honor the Liskov Substitution Principle?

I'm coming from a Java background, and I'm trying to wrap my head around Haskell's type system. In the Java world, the Liskov Substitution Principle is one of the fundamental rules, and I'm trying to understand if (and if so, how) this is a concept…
raner
  • 735
  • 4
  • 14
14
votes
1 answer

What is a Refused Bequest?

Could someone please explain what does Refused Bequest means? I tried reading some articles and says its a kind of code smell or in wiki it tells that it is a class that overrides a method of a base class in such a way that the contract of the base…
12
votes
4 answers

Overriding virtual boolean pure method without LSP breaking

For example we have the following structure: class Base { [pure] public virtual bool IsValid(/*you can add some parameters here*/) { //body } } class Child : Base { public override bool IsValid(/*you can add some…
12
votes
5 answers

Does the Liskov Substitution Principle apply to subtype which inherited from abstract class?

loosely speaking, Liskov Substitution Principle states that a derived class can be substitute in place of the base class without affecting the user. In the case when the base class is an abstract class, which means no user is using an instance of…
A.A.
  • 137
  • 7
12
votes
6 answers

How to comply with Liskov's Substitution Principle (LSP) and still benefit from polymorphism?

The LSP says "The derived types must not change the behavior of the base types", in other words "Derived types must be completely replaceable for their base types." This means that if we define virtual methods in our base classes, we have violated…
The Light
  • 24,407
  • 61
  • 164
  • 254
11
votes
4 answers

Is the Composite Pattern SOLID?

A Leaf in the Composite Pattern implements the Component interface, including Add, Remove, and GetChild methods that a Leaf is never going to use. This seems to be a violation of the Interface Segregation Principle. So is the usage of Composite…
10
votes
3 answers

The difference between liskov substitution principle and interface segregation principle

Is there any core difference between Liskov Substitution Principle (LSP) and Interface Segregation Principle (ISP)? Ultimately, both are vouching for designing the interface with common functionalities and introduce a new interface when you have…
10
votes
3 answers

Why Liskov Substitution Principle needs the argument to be contravariant?

One of the rules that Liskov Substitution Principle imposes on method signature in derived class is: Contravariance of method arguments in the subtype. If I understood correctly, it is saying that the derived class's overridding function should…
Mac
  • 1,561
  • 3
  • 10
  • 26
10
votes
2 answers

Overriding getPreferredSize() breaks LSP

I always see advices in this site of overriding getPreferredSize() instead of using setPreferredSize() as shown in these previous threads for example. Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size…
nachokk
  • 14,055
  • 4
  • 22
  • 47
10
votes
5 answers

Can't weakening preconditions and strengthening postconditions also violate Liskov Substitution Principle?

Actual precondition of a subtype is created by combining ( using logical OR ) preconditions of a base type and preconditions of a subtype, which makes the resulting precondition less restrictive Actual postcondition of a subtype is created by…
9
votes
2 answers

Liskov Substition and Composition

Let say I have a class like this: public sealed class Foo { public void Bar { // Do Bar Stuff } } And I want to extend it to add something beyond what an extension method could do....My only option is composition: public class…
FlySwat
  • 160,042
  • 69
  • 241
  • 308
9
votes
1 answer

Adapter Pattern vs Liskov Substitution

The Adapter design pattern is used to convert the interface of a class (Target) into another interface (Adaptee) clients expect. Adapter lets incompatible classes work together that could not otherwise because of their incompatible interfaces. The…
9
votes
2 answers

Avoiding of violating LSP

I want to separate data from source of the data. One class for database interaction and class for data manipulation. But my approach violates LSP: preconditions cannot be strengthened in a subtype and raises strict error: Declaration of…
sectus
  • 14,914
  • 5
  • 49
  • 88
9
votes
5 answers

C# Interface Implementation relationship is just "Can-Do" Relationship?

Today somebody told me that interface implementation in C# is just "Can-Do" relationship, not "Is-A" relationship. This conflicts with my long-time believing in LSP(Liskov Substitution Principle). I always think that all inheritance should means…
Morgan Cheng
  • 66,562
  • 63
  • 166
  • 223
9
votes
2 answers

Liskov substitution principle and Streams

Does the fact that there are Stream derived classes that cannot be written or sought break the Liskov substitution principle? For example, the NetworkStream cannot be sought, it will throw a NotSupportedException if the method Seek is called. Or…
vtortola
  • 32,045
  • 25
  • 144
  • 246
1
2
3
15 16