I need to create array of references to child objects. In this case Room has to have array of references to objects: DarkRoom and LightRoom. Having error in line where initialization of array of type Room. What’s missing?
public abstract class Room {
public Room[][] space = new Room[4][4]; // <<Syntax error on token ";",
space[0][0] = new DarkRoom();
space[0][1] = new LightRoom();
space[1][0] = new DarkRoom();
space[1][1] = new LightRoom();
}
public class LightRoom extends Room { ... }
public class DarkRoom extends Room { ... }
Your design is way off the mark. A class should not hold an array of child objects and in fact should have no knowledge about or dependence on its child classes. This is both a recursive and a logical nightmare.
I suggest you remove the array from within the Room class to somewhere more appropriate, such as the Hotel class or House class.