LSP states that classes should be substitutable for their base classes, meaning that derived and base classes should be semantically equivalent.
But does LSP also apply to classes implementing an interface? In other words, if an interface method implemented by a class is semantically different from what user expects it to be, would this be considered as a violation of LSP?
3
if an interface method implemented by a class is semantically different from what user expects it to be, would this be considered as a violation of LSP?
If the implementation is semantically different from the behavior documented through the invariants of the interface and its methods’ pre- and post-conditions, then the answer is “yes”, it would be a violation of the LSP. The principle establishes the rules for the abstraction and its implementations, without requiring the abstraction side to be present in the form of a class.
However, if we talk about what users expect, the answer would be “not necessarily”: the users are entitled to having wrong expectations.
3