Questions tagged [liskov-substitution]

For questions about Liskov substitution principle in object-oriented design.

Principle to design subtypes that are safe to substitute instead of supertype, that is code intended to work with supertype keeps functioning as expected when handling objects of subtype.

See also:

  • tag
  • Wikipedia article: Liskov substitution principle

    Substitutability is a principle in object-oriented programming. It states that, 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.). More formally, the Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called (strong) behavioral subtyping...

94 questions
150
votes
12 answers

Is this a violation of the Liskov Substitution Principle?

Say we have a list of Task entities, and a ProjectTask sub type. Tasks can be closed at any time, except ProjectTasks which cannot be closed once they have a status of Started. The UI should ensure the option to close a started ProjectTask is never…
Paul T Davies
  • 3,144
  • 2
  • 22
  • 22
61
votes
8 answers

LSP vs OCP / Liskov Substitution VS Open Close

I am trying to understand the SOLID principles of OOP and I've come to the conclusion that LSP and OCP have some similarities (if not to say more). the open/closed principle states "software entities (classes, modules, functions, etc.) should be…
58
votes
9 answers

Code Smell: Inheritance Abuse

It's been generally accepted in the OO community that one should "favor composition over inheritance". On the other hand, inheritance does provide both polymorphism and a straightforward, terse way of delegating everything to a base class unless…
dsimcha
  • 17,224
  • 9
  • 64
  • 81
54
votes
3 answers

What's the difference between a subclass and a subtype?

The highest rated answer to this question about the Liskov Substitution Principle takes pains to distinguish between the terms subtype and subclass. It also makes the point that some languages conflate the two, whereas others do not. For the…
tel
  • 569
  • 1
  • 4
  • 8
29
votes
9 answers

What can go wrong if the Liskov substitution principle is violated?

I was following this highly voted question on possible violation of Liskov Substitution principle. I know what the Liskov Substitution principle is, but what is still not clear in my mind is what might go wrong if I as a developer do not think about…
27
votes
14 answers

Is there a language or design pattern that allows the *removal* of object behavior or properties in a class hierarchy?

A well-known shortcoming of traditional class hierarchies is that they are bad when it comes to modeling the real world. As an example, trying to represent animals' species with classes. There are actually several problems when doing that, but one…
Sebastien Diot
  • 791
  • 6
  • 13
21
votes
6 answers

Does subclassing int to forbid negative integers break Liskov Substitution Principle?

In Python 3, I subclassed int to forbid the creation of negative integers: class PositiveInteger(int): def __new__(cls, value): if value <= 0: raise ValueError("value should be positive") return int.__new__(cls,…
21
votes
5 answers

Is method overriding always a violation of Liskov Substitution Principle?

Overriding a method originally defined in the super class, by definition means this method will do different things when invoked on an object of the base class or an object of the subclass. So does this mean that overriding methods always means…
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
20
votes
5 answers

Do special-cases with fallbacks violate the Liskov Substitution Principle?

Let's say I have an interface FooInterface that has the following signature: interface FooInterface { public function doSomething(SomethingInterface something); } And a concrete class ConcreteFoo that implements that interface: class…
Evan
  • 1,245
  • 1
  • 9
  • 18
19
votes
3 answers

How does strengthening of preconditions and weakening of postconditions violate Liskov substitution principle?

I read that Liskov's substitution principle is violated if : Preconditions are strengthened, or Postconditions are weakened But I don't get fully yet how these two points would violate Liskov substitution principle. Can some one please explain…
Geek
  • 5,107
  • 8
  • 40
  • 58
18
votes
3 answers

Is there a specific name for the "Square inherits from Rectangle" paradox?

A certain failure of OOP is shown with a class Square inheriting from Rectangle, where logically Square is a specialization of Rectangle and should therefore inherit from it, but everything falls apart when you attempt to change a Square's length or…
Victor
  • 947
  • 7
  • 12
18
votes
1 answer

Does Liskov Substitution Principle also apply to classes implementing an interface?

LSP states that classes should be substitutable for their base classes, meaning that derived and base classes should be semantically equivalent. But does LSP also apply to classes implementing an interface? In other words, if an interface method…
user1483278
  • 1,121
  • 1
  • 10
  • 14
17
votes
3 answers

Does the state Pattern violate Liskov Substitution Principle?

This image is taken from Applying Domain-Driven Design and Patterns: With Examples in C# and .NET This is the class diagram for the State Pattern where a SalesOrder can have different states during its life time. Only certain transitions are…
Songo
  • 6,548
  • 4
  • 48
  • 89
15
votes
6 answers

Can the circle-ellipse problem be solved by reversing the relationship?

Having Circle extend Ellipse breaks the Liskov Substition Principle, because it modifies a postcondition: namely, you can set X and Y independently to draw an ellipse, but X must always equal Y for circles. But isn't the problem here caused by…
HorusKol
  • 4,131
  • 1
  • 20
  • 25
15
votes
1 answer

How to verify the Liskov substitution principle in an inheritance hierarchy?

Inspired by this answer: Liskov Substitution Principle requires that Preconditions cannot be strengthened in a subtype. Postconditions cannot be weakened in a subtype. Invariants of the supertype must be preserved in a subtype. History…
1
2 3 4 5 6 7