StackP
StackP

Reputation: 31

Error using Java Classes/Inheritence

I'm having trouble with fixing this error. Can someone please help? My prompt and the code is posted below.

Write a super class encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length. It has two methods that calculate and return its area and volume. You also need to include a client class to test these two classes.

            public class Rectangle1
        {

        protected double width;
        protected double height;


        public Rectangle1(double width, double height){
        this.width = width;
        this.height = height;


        }

        public double getWidth(){
        return width;
        }

        public void setWidth(double width) {
        this.width = width;

        }
        public double getHeight(){
        return height;

        }

        public void setHeight(double height){
        this.height = height;

        }



        public double getArea(){
        return width * height;
        }

        public double getPerimeter(){
        return 2 * (width + height);

        }
        }

    public class Box extends Rectangle1 {
        protected double length;

        public Box(double length){
            this.length = length;
        }

        public double getLength(){
            return length;
        }

        public void setLength(double length){
            this.length = length;
        }

        public double getVolume(){
            return width * height * length;
        }
    }

    public class TestRectangle {

    public static void main(String[] args) {

    Rectangle1 rectangle = new Rectangle1(2,4);
    Box box = new Box(5);

    System.out.println("\nA rectangle " + rectangle.toString());
    System.out.println("The area is " + rectangle.getArea());
    System.out.println("The perimeter is " +rectangle.getPerimeter());
    System.out.println("The volume is " + box.getVolume());
    }
    }

The error is at

public Box(double length){
    this.length = length;
}

The error message in Eclipse IDE is as follows:

Implicit super constructor Rectangle1() is undefined. Must explicitly invoke another constructor.

And when I try to run it, it gives me:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Implicit super constructor Rectangle1() is undefined. Must explicitly invoke another constructor

    at Box.<init>(Box.java:4)
    at TestRectangle.main(TestRectangle.java:7)

Can someone please advise me on how to fix this error?

Upvotes: 1

Views: 1778

Answers (3)

Lloyd
Lloyd

Reputation: 11

Firstly, every subclass must call super(...) as the first statement in every constructor. This is a bit of a pain, so Java adds a call to super() at the start of any constructor that doesn't have a call to super(...). Since Rectangle1 doesn't have a constructor with no arguments, Java's attempt to call super() doesn't work and you need to add your own. Peter and Maroun covered this.

A bigger problem is that you haven't thought about what a Box is. What is a Box(5)? A Rectangle1 has a width and a height, while a Box has a width, a height and a depth. What is shape is a Box(5)? Your Box constructor should be something like

public Box (double width, double height, double depth)
{
    super (width, height);
    this.depth = depth;
}

In this constructor you can see that the arguments tell you everything you need to know about the Box and the call to super(height, width) takes care of delegating all the rectangle stuff to the base class.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533510

You have to call the super class constructor which you define. The default constructor only exists when you haven't defined one.

Also you should not attempt to initialise fields which are initialised by the parent as this breaks encapsulation. I suggest you do this.

public Box(double length){
    super(length, length);
}

This way you are calling a constructor in the super class you have defined and you let it set the fields it is responsible for.

Upvotes: 0

Maroun
Maroun

Reputation: 95958

Your base class Rectangle1 has a constructor:

public Rectangle1(double width, double height) {
    this.width = width;
    this.height = height;
}

Because you wrote a constructor, the default no aruments constructor will not exist, so super() will not find the right constructor. You should write: super(0, 0) in your Box constructor, which will match Rectangle1 constructor.

Upvotes: 1

Related Questions