In: Computer Science
// Base class for game configuration
public abstract class DataSource {
private Graph map;
private HashMap <String,Room> rooms;
private ArrayList <Entity> entities;
private Player player;
protected HashMap < String, List<String[]> > tables;
// constructor
public DataSource() {
}
// Connect to the data source. Override if source is a database
public void connect() {
};
// Load the configuration tables required to build the game world
public abstract void load();
// Build the game world
public final void build() {
// code omitted
// Disconnect from the data source. Override if source is a database
public void disconnect() {
};
// Get a the layout of the game world
public Graph getMap() {
return map;
}
// Get room details. The HashMap key is the room label.
public Map <String,Room> getRooms() {
return rooms;
}
// Get player details
public Player getPlayer() {
return player;
}
// Get entity (bats, bird, monsters, wumpus) locations
public List <Entity> getEntities() {
return entities;
}
}
}
. Explain how inheritance is intended to be used with this class. Does this represent a good use of inheritance?
Solution: It is not possible to directly instatiate an abstract class and in order to use this kind of class, you need to inherit it from another class and you would also have to provide the implementations to the methods that are present in this class. Now, coming to the question, if you really need to inherit an abstract class you can inherit it and then you are supposed to necessarily provide the implementation to all the abstract methods that are declared inside it.
For example, in the above case, you have the Data Source as the abstract class, and in order to access all of its member variables, you need to declare another class for example, Data class that extends the Data Source class and using this Data class you can access all the data of the Data Source class. This is the only and the most legit way of inheriting an abstract class and yes it makes good use of inheritance as you can implement the abstract methods according to your requirements.
Here's the solution to your question, please provide it a 100% rating. Thanks for asking and happy learning!!