There are tons of discussions out there about the LSP, but all of them seem to be excessively blurred.
AFAIK, LSP states that to properly override (not overload) a superclass method in a child, one should assure that the child method:
- does not yield a new type of exception that in no scenario is raised by the parent method
- has the same signature as the parent method (in case of strongly typed languages)
- has the same semantic MEANING of the signature
- returns value of the same type/types
- returns value of the same semantic meaning
By semantic meaning
I mean that if base class method implies that it returns int
and this int
means, say, USD or EUR, then the overloaded method should also imply that the returned value is either USD or EUR, and returning 'RUB' even in one scenario would violate LSP.
But what if my base class looks like this (example is in Python):
class A:
func(x: int) -> int
return x*2
class B(A):
func(x: int, y: string) -> int
return x*y
So there are two sub-questions in my question:
- What does the term
contract
mean in more practical terms ? Is it synonimous tointerface
? - Does overloading with a signature (in part of list of arguments) that is NOT present in the base class violate LSP?
Aucun commentaire:
Enregistrer un commentaire