views:

58

answers:

4

I came across the original statement of the Liskov Substitution Principle on Ward's wiki tonight:

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." - Barbara Liskov, Data Abstraction and Hierarchy, SIGPLAN Notices, 23,5 (May, 1988).

I've always been crap at parsing predicate logic (I failed Calc IV the first time though), so while I kind of understand how the above translates to:

Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.

what I don't understand is why the property Liskov describes implies that S is a subtype of T and not the other way around.

Maybe I just don't know enough yet about OOP, but why does Liskov's statement only allow for the possibility S -> T, and not T -> S?

A: 

the hypothetical set of programs P (in terms of T) is not defined in terms of S, and therefore it doesn't say much about S. On the other hand, we do say that S works as well as T in that set of programs P, and so we can draw conclusions about S and its relationship to T.

One way to think about this is that P demands certain properties of T. S happens to satisfy those properties. Perhaps you could even say that 'every o1 in S is also in T'. This conclusion is used to define the word subtype.

TokenMacGuy
A: 

As pointed by sepp2k there are multiple views explaining this in the other post. Here are my two cents about it. I like to look at it this way

If for each object o1 of type TallPerson there is an object o2 of type Person such that for all programs P defined in terms of Person, the behavior of P is unchanged when o1 is substituted for o2 then TallPerson is a subtype of Person. (replacing S with TallPerson and T with Person)

We typically have a perspective of objects deriving some base class have more functionality, since its extended. However with more functionality we are specializing it and reducing the scope in which they can be used thus becoming subtypes to its base class(broader type).

aeh
A: 

LSP is simple, it says that Drive class's object can be used perfecly in place of base class object.

So if S is T ==> T is base and S is drive.

and LSP principle works for Base down to Child not from Child to Base.

so what you asking for a REVERSE LSP which is not known yet.

saurabh
A: 

A derived class inherits its base class's public interface and is expected to either use the inherited implementation or to provide an implementation that behaves similarly (e.g., a Count() method should return the number of elements regardless of how those elements are stored.)

A base class wouldn't necessarily have the interface of any (let alone all) of its derived classes so it wouldn't make sense to expect an arbitrary reference to a base class to be substitutable for a specified derived class. Even if it appears that only the subset of the interface that supported by the base class's interface is required, that might not be the case (e.g., it could be that a shadowing method in the specific derived class is referred to).

Mark Cidade