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?
Thank you
No
It only applies to subtypes. See the Wikipedia article for a brief summary.
If you have a
class Bthat inherits or extendsclass Ayou should be able to switch outclass Awithclass Band everything should work as normal. Interfaces are often used in languages that do not allow for multiple inheritance, so while the two objects share a common behaviour, how that actually execute said behaviour is distinct between both, meaning you shouldn’t be able to switch them interchangeably.