Explore what CodeHS has to offer for districts, schools, and teachers.
Click on one of our programs below to get started coding in the sandbox!
View All
The Rectangle class is defined as follows:
Rectangle
public class Rectangle { private int width; private int height; public Rectangle(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return width; } public int getHeight() { return height; } public int getArea() { return width * height; } }
Which of the following properly defines a Square class that is a subclass of the Rectangle class? A Square is a Rectangle and has a width and height that happen to both be the same length.
Square
width
height
public class Square extends Rectangle { private Rectangle rectangle; public Square(int sideLength) { this.rectangle = new Rectangle(sideLength, sideLength); } }
public class Square extends Rectangle { private Rectangle rectangle; public Square(int sideLength) { this.rectangle = super(sideLength, sideLength); } }
public class Square extends Rectangle { private int width; private int height; public Square(int sideLength) { this.width = sideLength; this.height = sideLength; } }
public class Square extends Rectangle { public Square(int sideLength) { super(sideLength, sideLength); this.width = sideLength; this.height = sideLength; } }
public class Square extends Rectangle { public Square(int sideLength) { super(sideLength, sideLength); } }