1
public class TestingClass {


    public static void main(String[] args) {

        int numberRooms = 6;
        double totalSpace;
        Room[] rooms = new Room[numberRooms];
        rooms[0] = new Room(20, 20);
        rooms[1] = new Room(20, 20);
        rooms[2] = new Room(22, 20);
        rooms[3] = new Room(20, 20);
        rooms[4] = new Room(25, 20);
        rooms[5] = new Room(15, 13);

         Garage garage = new Garage(20, "Cement", 1, 20, 1, 4);

        for (int i = 0; i < numberRooms; i++) {
            totalSpace = totalSpace + calculateRoomSpace(rooms[i]);
        }

        System.out.println("Total room space is " + totalSpace);

        foo(garage);

    }

    public static double calculateRoomSpace(Room roomSpace) {
        double newRoomSpace = roomSpace.calcFloorSpace();
        return newRoomSpace;
    }

    public static void foo(Building myBuilding) {
        System.out.println("Total floor space is " + myBuilding.calcFloorSpace());
    }
}

I put in this code and get the error

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Cannot instantiate the type Garage

    at TestingClass.main(TestingClass.java:17)"

what is the problem here exactly?

Edit ** Here is the Garage class**

abstract public class Garage extends Building {


    private int cars;
    private String floorType;
    private int garageLength;
    private int garageWidth;

    public Garage(int floors, int windows, int cars, String floorType,
            int garageLength, int garageWidth) {
        super(floors, windows);
        this.cars = cars;
        this.floorType = floorType;
        this.garageLength = garageLength;
        this.garageWidth = garageWidth;
    }

    @Override
public double calcFloorSpace() {
    int floorSize;
    floorSize = garageLength * garageWidth;
    return floorSize;
}

    public int getCars() {
        return cars;
    }

    public void setCars(int cars) {
        this.cars = cars;
    }

    public String getFloorType() {
        return floorType;
    }

    public void setFloorType(String floorType) {
        this.floorType = floorType;
    }

    public int getGarageLength() {
        return garageLength;
    }

    public void setGarageLength(int garageLength) {
        this.garageLength = garageLength;
    }

    public int getGarageWidth() {
        return garageWidth;
    }

    public void setGarageWidth(int garageWidth) {
        this.garageWidth = garageWidth;
    }

    @Override
    public String toString() {
        return "Garage [cars=" + cars + ", floorType=" + floorType
                + ", garageLength=" + garageLength + ", garageWidth="
                + garageWidth + "]";
    }

}

Hope this helps. Not an expert at this and have spent quite a while figuring this out. thanks

Ruchira Gayan Ranaweera
  • 32,406
  • 16
  • 66
  • 105
Shani
  • 13
  • 1
  • 4

4 Answers4

3

You can't instantiate an abstract class.

You've got two options:

  • Remove abstract from the class declaration, or
  • Create another class to extend Garage, which can be instantiated.
Makoto
  • 96,408
  • 24
  • 164
  • 210
2

You cannot instantiate an instance of an abstract class. You either need to make your class concrete (remove the abstract modifier on the class and provide an implementation for all methods), or extend the abstract class to create a non-abstract class.

When you remove the abstract modifier, the compiler will tell you what abstract methods you are missing (if any).

Rob
  • 5,634
  • 1
  • 21
  • 28
2

Your Garage Class is declared as abstract class cannot be instantiated .Check this for more details Can an abstract class have a constructor?

Community
  • 1
  • 1
Nargis
  • 4,562
  • 1
  • 25
  • 44
0

Garage is an abstract class, so it cannot be instantiated. Only its subclasses can be instantiated.

tbodt
  • 15,199
  • 5
  • 52
  • 78