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.

295 questions
-1
votes
1 answer

Object-oriented paradigms (SOLID) violated in Python? (example: subclassing of tuple to implement FractionTuple)

I was quite surprised to see that Python - at least partially - seems to violate some oo / SOLID principles. Example: class FractionTuple(tuple): def __new__(cls, p, q): # Ensure that p and q are integers if not isinstance(p, int) or not…
TomS
  • 216
  • 1
  • 7
-1
votes
1 answer

How can I make this small pseudocode comply with Liskov principle?

Keeping things tidy and simple, here is my initial Java pseudocode as an example: public abstract class Vehicle { private String owner; private id plate; public removeVehicle() { if (typeof(this) == Car)…
-1
votes
2 answers

What should be the result of overridden "Object#equals(Object)" when instance is upcasted?

I am, specifically concerned with obeying the symmetry part of the general contract established in Object#equals(Object) where, given two non-null objects x and y, the result of x.equals(y) and y.equals(x) should be the same. Suppose you have two…
-1
votes
2 answers

Liskov Substitution Principle and strengthened validation?

public abstract class Entity { public abstract IList Values { get; set; } } public class GenericEntity : Entity { private IList _Values; public override IList Values { …
Ilya Loskutov
  • 1,967
  • 2
  • 20
  • 34
-2
votes
2 answers

Does the wrong interface implementation violate Liskov Substitution Principle?

Let's consider the following interface implementation: Comparator stringComparator = (o1, o2) -> 0; Does it violate the Liskov Substitution Principle?
Daniel
  • 109
  • 1
  • 7
-2
votes
1 answer

Fulfill Liskov substitution principle

I have two classes: public class Base { private final int value; public Base(int value){ this.value = value; } public int getValue(){ return value; } public Base divide(Base that){ return new…
binaryBigInt
  • 1,526
  • 2
  • 18
  • 44
-2
votes
2 answers

Understanding Liskov Substituion Principle

My sample program like below; public class Animal { public virtual string MakeSound() { return "General Sound"; } } public class Dog : Animal { public override string MakeSound() …
Akhil
  • 1,918
  • 5
  • 30
  • 74
-2
votes
1 answer

Does using virtual methods violates LSP( L part of SOLID principles) or there are some exceptions?

Does using virtual methods violates LSP( L part of SOLID principles) or there are some exceptions? Thanks in advance, Saghar Ayyaz
-2
votes
1 answer

Is this a LSP violation (php)?

class Message { protected $body; public function setBody($body) { $this->body = $body; } public function getBody() { return $this->body; } } class ExtendedMessage extends Message { private…
-4
votes
1 answer

Why does InflaterInputStream#available() violates Liskov Substitution Principle?

From Android Reference: Although consistent with the RI, this behavior is inconsistent with available(), and violates the Liskov Substitution Principle. This method should not be used. Why and how does this method violates the principle? As a…
kolistivra
  • 4,229
  • 9
  • 45
  • 58
1 2 3
19
20