Question

In: Computer Science

// Base class for game configuration public abstract class DataSource {     private Graph map;    ...

// 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?

Solutions

Expert Solution

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!!


Related Solutions

Assume you have created the following data definition class: public abstract class Organization { private String...
Assume you have created the following data definition class: public abstract class Organization { private String name;    private int numEmployees;    public static final double TAX_RATE = 0.01;    public String getName() { return this.name; }    public int getNumEmployees() { return this.numEmployees; }         public abstract double calculateTax() {       return this.numEmployees * TAX_RATE;    } } In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>();...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>(); } public void addEdge(String v, String w) { // Put v in w's SET and w in v's SET. if (!st.contains(v)) st.put(v, new SET<String>()); if (!st.contains(w)) st.put(w, new SET<String>()); st.get(v).add(w); st.get(w).add(v); } public Iterable<String> adjacentTo(String v) { return st.get(v); } public Iterable<String> vertices() { return st.keys(); } // See Exercises 4.5.1-4 for V(), E(), degree(), // hasVertex(), and hasEdge(). public static void main(String[] args)...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
C++ Programming 1) What is the feature that makes a base class an abstract class? 2)...
C++ Programming 1) What is the feature that makes a base class an abstract class? 2) When designing a class hierarchy involving classes A and B, what are the two questions that must be asked to determine whether class B should be derived from class A?
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
// FILE: table1.h // // ABSTRACT BASE CLASS: Table //    1. The number of records...
// FILE: table1.h // // ABSTRACT BASE CLASS: Table //    1. The number of records in the Table is stored in total_records // 2. The hashcode function returns a location in the table for the // input key. It calls hash function in functional library. // 3. insert and print are two virtual functions to be overridden // insert: Add a new record into the Table; //           If key is already in the table, do nothing //...
Is a college class at a public university a Private good, Public good, or Club good
Is a college class at a public university a Private good, Public good, or Club good
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT