Daniel Stables
Daniel Stables

Reputation: 13

Cannot instantiate the type Rectangle

I am currently studying java. One of my assignments requires us to write some inheritance code that will display a rectangle in the console of Eclipse. I keep getting the error in my title when trying to run it but can't figure out what I'm doing wrong. Any tips or guidance would be appreciated.

TestShape.java

public class TestShape {

    public static void main(String[] args) {

        double width = 5, length = 7;
        Shape rectangle = new Rectangle(width, length);
        System.out.println("Rectangle width: " + width + " and length: " + length
                + "\nResulting area: " + rectangle.area()
                + "\nResulting perimeter: " + rectangle.perimeter() + "\n");
    }

}

Shape.java

public abstract class Shape {

        public abstract double area();
        public abstract double perimeter();
        public double getArea() {
            // TODO Auto-generated method stub
            return 0;
        }
        public double getPerimeter() {
            // TODO Auto-generated method stub
            return 0;
        }

}

Rectangle.java

    public abstract class Rectangle extends Shape {

    private final double width, height, area, perimeter;

  public Rectangle(double width, double height) {

    this.width = width;

    this.height= height;

    this.area = width * height;

    this.perimeter = 2 * (width + height);
  }

  @Override

  public double getArea() {

    return this.area;

  }

  @Override

  public double getPerimeter() {

    return this.perimeter;

  }

}

Upvotes: 0

Views: 1425

Answers (2)

Blasanka
Blasanka

Reputation: 22437

The thing is you cannot instantiate abstract class:

Shape rectangle = new Rectangle(width, length);

And you cannot remove abstract keyword in Rectangle class because two methods are did not implemented in Shape class.

public abstract double area();
public abstract double perimeter();

To instantiate you need to create concreate class or can implement all the method in Rectangle class, which methods are inherited from Shape class.

Since area() and perimeter() are features of Rectangle class you can implement those two methods and remove abstract from Rectangle class(now this class is a concrete class). Then, you can instantiate Rectangle class in main.

Upvotes: 2

kk.
kk.

Reputation: 3945

As you have defined 2 abstract methods in Shape class, these needs to be either implemented by Rectangle class or Rectangle class needs to be abstract. If you make Rectangle abstract, you will not be able to instantiate. In order to get your code working with minimal changes please chhange your Rectangle class implementation to:

public class Rectangle extends Shape {

    private final double width, height, area, perimeter;

    public Rectangle(final double width, final double height) {
        this.width = width;
        this.height = height;
        this.area = width * height;
        this.perimeter = 2 * (width + height);
    }

    @Override
    public double getArea() {
        return this.area;
    }

    @Override
    public double getPerimeter() {
        return this.perimeter;
    }

    @Override
    public double area() {
        return getArea();
    }

    @Override
    public double perimeter() {
        return getPerimeter();
    }
}

Upvotes: 1

Related Questions