Smitha Reddy
Smitha Reddy

Reputation: 1

Liskov substitution design principle cannot be achieved?

Please give me example where "Liskov substitution" design principle cannot be achieved?

Thanks.

Upvotes: 0

Views: 193

Answers (2)

David Brower
David Brower

Reputation: 3048

A scenario where the LSP conceivably could not be achieved is where you might have two requirements for a Class A: (1) that it inherit from Class B or implement Interface C and (2) that it not provide some of the behaviour provided by Class B or to which Interface C contracts it.

An example of a class that violates the Liskov Substitution Principle can be found in the .NET Framework's ReadOnlyCollection<T> which implements ICollection<T> but does not provide the required behaviour of Add or Remove. Clearly, it would not make sense for ReadOnlyCollection<T> to permit records to be added or removed but I suspect that there were also compelling reasons for it to implement ICollection<T>.

Upvotes: 1

weston
weston

Reputation: 54801

Suggest you read up on the circle eclipse problem.

Basically LSP is tricky to enforce when you add additional conditions to sub types.

In the case of the circle it is an extra condition that width must equal height but the system, that only knows about the superclass eclipse, expects to be able to modify them independently.

Note this is not an issue if the eclipse has no setters on width or height. As the system using the eclipse can't modify them the circle doesn't alter the eclipse contract.

Upvotes: 0

Related Questions