vendredi 22 mai 2020

Does overloading (not overriding) break the Liskov subsitution principle?

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:

  1. does not yield a new type of exception that in no scenario is raised by the parent method
  2. has the same signature as the parent method (in case of strongly typed languages)
  3. has the same semantic MEANING of the signature
  4. returns value of the same type/types
  5. 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:

  1. What does the term contract mean in more practical terms ? Is it synonimous to interface ?
  2. 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