Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

A Square is a Rectangle, right?

+5 votes
So I was just reviewing my homework that was graded and noticed I got this problem wrong,

"Because a square is a rectangle, a Square class should be a subclass of Rectangle class that has setter methods to change the shape's size.‚Äč"

I selected true because I believed Square to be a subclass of rectangle, since Square is a Rectangle. Could someone help me understand why this is false?
asked Aug 30 in CSC 285 (F17) by Daniel Zwiener (100 points)

1 Answer

+2 votes
 
Best answer
I also said true because a square is a rectangle. I think either the question was wrong, or the issue has to do with the fact that the setter methods Square inherits from Rectangle don't work for Squares because if you setHeight() then the width should also be set to the same thing, and vice versa. I kind of figured the best choice would be to override the setHeight() and setWidth() methods to adjust both dimensions when they were called, but maybe that's bad form?
answered Aug 30 by Vecna (100 points)
selected Sep 21 by Daniel Zwiener
I had the same thought, like when you override setHeight() you could make it set the value of Width as well to make sure it stays a square.
Yep, that is "bad form".  See page 559 in the HF book. (which, incidentally, was part of the reading assignment that this reading quiz was covering...  pp. 558-565 in appendix i)
...