Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I usually try to make sure my object instances comply with the Liskov Substitution Principle, but I've always wondered is do people think LSP should apply to constructors too?

I've tried googling for this but I haven't been able to find any strong opinions either way.

I should note that most of my coding is in Ruby, but I sometimes find that my subclass constructors are slightly different from the parent class. They take the same base set of arguments, and often extra args. Sometimes this also happens with other class methods.

In the back of my head this has always felt like an LSP violation, but I wanted to see if anyone else feels this way too.

like image 684
dkubb Avatar asked Mar 30 '11 18:03

dkubb


People also ask

What violates Liskov Substitution Principle?

A very common violation of this principle is the partial implementation of interfaces or base class functionality, leaving unimplemented methods or properties to throw an exception (e.g. NotImplementedException).

Which of the following are violations of LSP?

LSP violations symptomsDerivates that override a method of the base class method to give it completely new behaviour. Derivates that override a method of the superclass by an empty method. Derivates that document that certain methods inherited from the superclass should not be called by clients.

What is the most accurate example of Liskov Substitution Principle?

The classic example of the inheritance technique causing problems is the circle-elipse problem (a.k.a the rectangle-square problem) which is a is a violation of the Liskov substitution principle. A good example here is that of a bird and a penguin; I will call this dove-penguin problem.

How do you apply the Liskov Substitution Principle?

To understand the Liskov Substitution Principle, we must first understand the Open/Closed Principle (the “O” from SOLID). The goal of the Open/Closed principle encourages us to design our software so we add new features only by adding new code.


2 Answers

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.

like image 163
Philip JF Avatar answered Sep 23 '22 11:09

Philip JF


Definitely No.

Constructors are normally specialized for subtypes. Trying to apply LSP to constructors would be like saying subtypes can't have added specific methods or members. But the restriction is only the other way around.

And I also agree with Philip, constructors are not really part of a type (and in some languages you can easily use other factories instead of constructors). Using smalltalk terminology you would say constructors are methods of meta-classes.

No violation of LSP here, it only applies to instance methods, not to class methods (constructors or any other class methods).

like image 36
kriss Avatar answered Sep 24 '22 11:09

kriss