Question #95983

Write a program to define a class Shape with data members row and column. The superclass has a constructor that used to set row and column and method rectangle() with access specifier ‘protected’ which used to display the shape of rectangle.

Expert's answer

class Shape {
    private int row;
    private int column;

    public Shape(int row, int column){
        this.row = row;
        this.column = column;
    }

    protected void rectangle(){
        if(this.row == this.column)
            System.out.println("This shape is a square.");
        else
            System.out.println("This shape is a rectangle.");
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS