X
X
Xveeder2022-03-17 18:55:39
OOP
Xveeder, 2022-03-17 18:55:39

Does redefining a constructor violate the Liskov substitution principle in particular cases?

Let's say we have a base interface that only defines behavior and some base properties, but doesn't define a constructor signature.

This entity is supposed to be delivered to clients via dependency injection (either directly or through a factory). That is, instantiation of an object is always out of bounds for clients by definition. In this case, if the children of this entity have specific constructors with specific signatures, will this violate the LSP?

After all, all clients will be tied strictly to the interface of an already instantiated object.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-03-17
@Xveeder

No, it doesn't. The Liskov substitution principle says that the client should not see the difference between the base object (in your case, the interface) and its descendant. That is, descendants must comply with the contract defined by the ancestor (they can make it stricter, but not weaker).
The constructor is not part of the interface, so redefining the constructor will not violate the Liskov substitution principle in any way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question