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
0
votes
2 answers

Liskov Substitute Principle (LSP) with Code example

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 constraint (the "history rule").…
Sam
  • 19
  • 3
0
votes
1 answer

ID and EAN inheritance may violate Liskov Substitution Principle

I have a design question for my application. I have a product and a category. Both must have an ID. Category can have any positive integer as an ID. (-> Id-class) Product must have an positive integer between 8 & 13 ciphers as an ID. (->…
0
votes
1 answer

Inheriting a C++ interface and LSP violation

I have an interface like this class IVersion { public: virtual char * get_version() const = 0; virtual bool is_compatible(const IVersion& other) const = 0; }; I have a set of "Device" classes, all of which need to have the above interface…
Arun
  • 3,138
  • 4
  • 30
  • 41
0
votes
0 answers

LSP : Inheritance vs Composition

I need to respect the LSP while supporting multiple data types (templates can't be used) and I have a dilemma between inheritance and composition. If I use inheritance, the base class cannot be used by itself, it must be casted everytime. Does that…
0
votes
1 answer

Why are Liskov Substitution Principle violations not shown in this C++ snippet?

I was trying out violations of the Liskov Substitution Principle using C++ class inheritance but could not replicate the same problem resulting from LSP violation that was demonstrated with a Java program. The source of the Java program can be found…
zephinzer
  • 1,077
  • 11
  • 9
0
votes
1 answer

How do i load a lsp file from standalone C# application into AutoCAD and waiting for it to finish

first time poster here. I'm building a standalone application in C# where I can drop a set of .dwg's and select which .lsp files I want to run on that set. I can figure out how to open a dwg, load a .lsp file, save the dwg and close it. What I can't…
0
votes
1 answer

Why use type substitution

Can anyone explain to me what is the need of using type substitution? e.g. class Circle extends Shape{ ... } . . . class Main{ public static void main(String[] args){ Shape s = new Circle(); ... } } What possible benifit…
bytestorm
  • 1,411
  • 3
  • 20
  • 36
0
votes
2 answers

PHP Strict standards: What is the non-compliance in this code?

If i write this code in PHP with Stricts Standarts, i've an non-compliance: class Readable {}; class Atom extends Readable {}; class Reader { public function read(Readable $readable){} } class AtomReader extends Reader { public function…
bux
  • 7,087
  • 11
  • 45
  • 86
0
votes
1 answer

Liskov Substitution Principle different data types

I have a problem with the LSP in my program. I have a Base Class that is abstract and two different types of products , one is Food , the other is an Electronic device. I need to make a method that returns the expirationDate/Warranty for those…
SCBbestof
  • 34
  • 1
  • 6
0
votes
1 answer

Forcing a syntax error on automatic ToString() method

By default in c# all classes inherit the ToString() method. The problem I'm having is that at work we are using the automapper to map some domain objects to the front end. I keep seeing code very similar to the following sudo. string:mapToclass.name…
Lee
  • 564
  • 7
  • 16
0
votes
2 answers

Liskov Substitution Principle and Redundant Methods

I have an interface called IRepository. This interface defines a set of generic methods such as: IQueryable Get() where T : class; void Add(T obj) where T : class; void Update(T obj) where T : class; void SaveChanges(); I then have…
0
votes
1 answer

Does LSP also make sense for dynamic typed language like Ruby?

Consider the classic example in Java // Violation of Likov's Substitution Principle class Rectangle { protected int m_width; protected int m_height; public void setWidth(int width){ m_width = width; } public void…
mko
  • 21,334
  • 49
  • 130
  • 191
0
votes
1 answer

Avoiding LSP (Liskov Substitution Principles) violations

I'm currently reading the "Working Effectively with Legacy Code" by Michael Feathers and I think I understand about the LSP violations, but the thing is it says something about the rules of thumb that help avoiding LSP violations which…
Quizer
  • 471
  • 5
  • 17
0
votes
1 answer

How to ensure inheriting classes do not break base class contract in the context of LSP?

In C# - but it may be applicable to other languages - in the context of LSP, how can I ensure that a class inheriting from another (mutable) one will not break the original contract? For example: If I have public, internal or protected property…
Guillaume
  • 1,782
  • 1
  • 25
  • 42
0
votes
2 answers

Why can't I use AddRange to add subclassed items?

I have two classes.... Parcel and FundParcel...and I'm trying to convert an IEnumerable of the subtype to an IList of the supertype.... public class FundParcel : Parcel { /* properties defined here */ } public class Parcel { /* properties…
mezoid
  • 28,090
  • 37
  • 107
  • 148
1 2 3
19
20