Should constructors comply with the Liskov Substitution Principle? [closed]

No, when you use a constructor you know you are dealing with the subtype. This allows you to have preconditions not required for the parent constructor such as other parameters. This is why in most languages the constructor name is that of the class being created.

A good example of how this is that a ColoredSquare could be a proper subtype of Square, but requires an extra parameter: color. If you couldn’t do things like this subtypes would be much less useful.

In some sense, the constructor isn’t really part of the type: it is a function that returns an element of that type. Thus, defining a new constructor for a subtype, doesn’t break LSP.

Leave a Comment