Do Collections.unmodifiableXXX methods violate LSP? [closed]

LSP says that every subclass must obey the same contracts as the superclass. Wether or not this is the case for Collections.unmodifiableXXX() thus depends on how this contract reads. The objects returned by Collections.unmodifiableXXX() throw an exception if one tries to call any modifying method upon them. For instance, if add() is called, an UnsupportedOperationException … Read more

Is deriving square from rectangle a violation of Liskov’s Substitution Principle? [closed]

The answer depends on mutability. If your rectangle and square classes are immutable, then Square is really a subtype of Rectangle and it’s perfectly OK to derive first from second. Otherwise, Rectangle and Square could both expose an IRectangle with no mutators, but deriving one from the other is wrong since neither type is properly … Read more

Why array implements IList?

Because an array allows fast access by index, and IList/IList<T> are the only collection interfaces that support this. So perhaps your real question is “Why is there no interface for constant collections with indexers?” And to that I have no answer. There are no readonly interfaces for collections either. And I’m missing those even more … Read more