If I have something like
class square : figure {}
class triangle : figure {}
Does that mean that I should never ever use the square and triangle classes but only refer to figure?
Like never do like this:
var x = new square();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In your case, LSP would mean that all behavior inherited from
figureshould be appropriate for asquareortriangle. So you wouldn’t want to have setters forfigure.Side1,Side2andSide3because those wouldn’t make sense for asquare.At some point you’ll have to refer to
squareortriangle, but only in cases where what you’re doing is specific to the subclass. If you’re implementing behavior that will apply equally well for all figures (maybe a Draw method), then it should accept afigureparameter rather than asquareortriangle.As an example, your classes might be set up as follows:
As long as
figure.draw()is virtual, meaning its implementation can (or must) be overriden by a subclass, you can executetriangle‘sdraw()behavior even if the object is being used as afigure.