Liskov Substitution Principle (LSP) examples

I'm studying LSP fundamental design rule. In the slides I'm reading there are the following statements:
a) A SortedList is not a List
c) A ColoredPoint is not a Point

I don't understand this assumptions...
This is my understanding for each case:
a) This breaks the rule of the Design per Contract (DbC) because the pre-condition is stronger in the SortedList than in List. However everywhere where an object or List is expected a SortedList can be passed as the LSP rule says. So why a SortedList is not a List?
b) The same. For me breaks the rule of DbC because the pre-condition goes stronger. However everywhere where an object of Point is expected a ColoredPoint can be passed.
> So why a SortedList is not a List?

A SortedList is a List.

However, a valid operation on a list (for example append some arbitrary new items at the end of the list, or swap the element at the front with one at the back) need not be a valid operation on a sorted list.

So a list can't be substituted with a sorted list. For example, we shouldn't be able to pass a sorted list to a function that expects a reference or pointer to a non-const (unordered) list.
Last edited on
Topic archived. No new replies allowed.