3 ms·
To correctly apply the LSP when designing subclasses it's not enough to just ensure syntactic safety but also behavioral correctness. The Bag example you gave i
by tcgv 6y ago
To correctly apply the LSP when designing subclasses it's not enough to just ensure syntactic safety but also behavioral correctness. The Bag example you gave is an example of not ensuring behavioral correctness.
> Doing the opposite is safe, of course, but the code reuse doesn't work when you make your Bag a subtype of Set
I disagree. suppose the Bag class only allows unique entries, then if you create a subclass that overrides this behavior to allow duplicates you are breaking an expectation the system may have about Bags, and possibly relies on, provoking unexpected consequences. For instance, allowing duplicates in Bags may trickle down to an inventory shortage issue since the system plans at most one item of each type per user.