3

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

user702769
  • 2,435
  • 2
  • 25
  • 34
  • 1
    yes, at least in java. http://stackoverflow.com/questions/7072290/in-what-ways-are-subtypes-different-from-subclasses-in-usage – Ray Tayek Oct 16 '12 at 19:27
  • @Ray Tayek: If it would be considered violation in Java, then I assume it would also be violation in C#? Anyways, you're saying just the opposite of what Finglas is saying - now I'm a bit confused – user702769 Oct 16 '12 at 20:20
  • 1
    depends on your definition of http://en.wikipedia.org/wiki/Subtype_polymorphism – Ray Tayek Oct 16 '12 at 21:20
  • @Ray Tayek: I can understand that people use the term Subtype polymorphism differently, but how does ones definition/understanding of the term "Subtype polymorphism" have anything to do with LSP? – user702769 Oct 16 '12 at 22:26
  • 1
    if an implementation is a subtype, then lsp should be true for interfaces – Ray Tayek Oct 17 '12 at 00:19
  • @Ray Tayek: ok, last question - in your opinion, is ( in java/C# ) an implementation of an interface always sort of a subtype of an interface or does it depend on the situation? – user702769 Oct 17 '12 at 00:52
  • 1
    i an not a c# expert, but i believe that in both cases an implementation is a subtype. any implementation may or may not violate lsp. see http://en.wikipedia.org/wiki/Circle-ellipse_problem – Ray Tayek Oct 17 '12 at 01:43

2 Answers2

1

No

It only applies to subtypes. See the Wikipedia article for a brief summary.

If you have a class B that inherits or extends class A you should be able to switch out class A with class B and 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.

Finglas
  • 15,518
  • 10
  • 56
  • 89
  • 1 - "It only applies to subtypes." This is first time I hear of the term "subtype". So (in C# at least) a class implementing an interface is not considered a subtype of that interface? 2 - Why ( according to Ray Tayek ) would in Java this be considered a violation of LSP? – user702769 Oct 16 '12 at 20:18
  • 1
    It's a bit fuzzy in terminology. People tend to use subtype/subclass interchangeably. I was referring to an "is a" relationship. That's how I see the LSP, based on inheritance. It would be interesting to hear other points of view however. Regardless, the LSP is just that, a principle. Not a rule. Apply it where required. – Finglas Oct 16 '12 at 22:08
1

Yes. Interfaces have an "is an [noun]" relationship just like classes, except that the noun is not a concrete type, but rather a "thing which is [adjective phrase]". If the adjective phrase is "capable of being safely asked if it can accept items, and either capable of accepting items or reporting that it won't", then any object which could not safely be asked if it could accept items, or which might answer yes but then behave badly if actually given an item, would not be a legitimate implementation of the interface described thereby.

supercat
  • 77,689
  • 9
  • 166
  • 211