I have a class an abstract class Room
which has subclasses Family
and Standard
, I have created room = new ArrayList<Room>();
within a Hostel
class. I have a method to add a room to the ArrayList;
public String addRoom(String roomNumber, boolean ensuite)
{
if (roomNumber.equals(""))
return "Error - Empty name field\n";
else
room.add( new Room(roomNumber,ensuite) );
return "RoomNumber: " + roomNumber + " Ensuite: " + ensuite
+ " Has been added to Hostel " + hostelName;
}
However I get the compile time error;
Room is abstract; cannot be instantiated
I understand that abstract classes cannot be instantiated, but what is the best way to add rooms?
You have this error because you are trying to create an instance of abstract class, which is impossible. You have to
room.add(new Family(roomNumber, ensuoute));
or
room.add(new Standard(roomNumber, ensuoute));