Answer:
The java program including classes is shown below.
import java.util.*;
import java.lang.*;
//base class
class Rectangle1
{
//variables to hold dimensions and area
static double l;
static double b;
static double a;
//setter
static void setWidth(double w)
{
b=w;
}
//getter
static double getWidth()
{
return b;
}
//setter
static void setLength(double h)
{
l=h;
}
//getter
static double getLength()
{
return b;
}
//area calculation for rectangle
static void computeArea()
{
a = getLength()*getWidth();
System.out.println("Area of rectangle = " +a);
}
}
//derived class
class Square1 extends Rectangle1
{
//variables to hold dimensions and perimeter
static double s;
static double peri;
//settter
static void setSide(double d)
{
s=d;
}
//getter
static double getSide()
{
return s;
}
//perimeter calculation for square
static void computePerimeter()
{
peri = 2*(getSide()+getSide());
System.out.println("Perimeter of square = "+peri);
}
}
public class MyClass {
public static void main(String args[]) {
//object of derived class created
Square1 ob = new Square1();
ob.setLength(1);
ob.setWidth(4);
ob.setSide(1);
ob.computeArea();
ob.computePerimeter();
}
}
Step-by-step explanation:
1. The class for rectangle is defined having variables to hold the dimensions of a rectangle.
2. The getters and setters are included for both the dimensions.
3. The method to compute the area of the rectangle is defined.
4. The class for square is defined having variables to hold the side of a square.
5. The getter and setter are included for the dimension of the square.
6. The method to compute the perimeter of the square is defined.
7. The class Square1 is the derived class which inherits the class Rectangle1 through the keyword extends.
8. All the variables, getters, setters and methods are declared as static.
9. Another class is defined having only the main() method.
10. Inside main(), the object of the derived class, Square1, is created.
11. The setters and the methods are called through this object.
12. The value of the dimensions are passed as arguments to the setters.
13. The output is attached in an image.