Inheritance in Java Instructions: Construct a class called “Rectangle”. A “Rectangle” has both a length and a width (both positive integers). Access to them should be only inside the class and within the inheritance hierarchy. Then implement the following: - lone constructor that accepts two integers for the length and the width. Prints “Rectangle Constructor” getters and setters - area - prints “Rectangle Area” and returns the area of the rectangle - perimeter - prints “Rectangle Perimeter” and returns the perimeter of the rectangle Create a class called “Square”. The square is a rectangle. Therefore, have Square inherit from Rectangle. Square construction prints “Square Constructor” and accepts one integer which is the length of a side. The version of the area and perimeter of Square prints “Square Area” and “Square Perimeter”, respectively. Inputs A positive integer representing the length of the side of a Square. 2
Inheritance in Java
Instructions:
Construct a class called “Rectangle”. A “Rectangle” has both a length and a width (both positive integers). Access to them should be only inside the class and within the inheritance hierarchy. Then implement the following:
- lone constructor that accepts two integers for the length and the width. Prints “Rectangle Constructor”
getters and setters
- area - prints “Rectangle Area” and returns the area of the rectangle
- perimeter - prints “Rectangle Perimeter” and returns the perimeter of the rectangle
Create a class called “Square”. The square is a rectangle. Therefore, have Square inherit from Rectangle. Square construction prints “Square Constructor” and accepts one integer which is the length of a side. The version of the area and perimeter of Square prints “Square Area” and “Square Perimeter”, respectively.
Inputs
A positive integer representing the length of the side of a Square.
2
Sample Output
Enter side length: 2
Rectangle Constructor
Square Constructor
Square Area
Area: 4
Square Perimeter
Perimeter: 8
Step by step
Solved in 4 steps with 4 images