Expert Answers

You are developing a simple graphics package that will allow you to draw and manipulate simple geometric shapes such as squares, rectangles, etc. Your initial design is along the following lines:

public class Rectangle {
private int h, w;
public int getHeight() {…}
public int setHeight(int x) {…}


public class Square extends Rectangle {
// ???
This question has a ‘devil’s advocate’ nature to it and is in two parts.

You intend to model a square as inheriting from a rectangle. How would you complete the code on the above right to realize this intention?

No matter how you do design the code for the square, you will be able to write code that will break the meaning of what it means to be a square or rectangle. After completing Part a, discuss the flaw in your design and outline some code that will cause it to break (your code should compile fine and will also execute without an error but will violate how a rectangle or square should behave).