Question

In: Computer Science

Consider the DataSource class in below code. Explain how inheritance is intended to be used with...

Consider the DataSource class in below code. Explain how inheritance is intended to be used with this class. Does this represent a good use of inheritance? Explain your answer

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

    }  

}

Solutions

Expert Solution

Inheritance

Yes, this class represents good use of inheritance.

Some unnecessary things you need to remove are:

1. unnecessary semi-colon at end of connect method
2. same with disconnect method

Some things you need to add:

1. Since there is no built-in(primitive) datatype named 'Player' in java, so you definitely have to create Player class.
2. Same, you'd have to create Room class

Also, it has load method which is abstract which would be implemented by child classes.

An example of how to use it (in child class) is as follows:-

public DataBase extends DataSource {

   /*
       We can access protected field which is tables in our case
       We cannot access other fileds, just can access public methods
   */

   public static void main(String[] args) {

   }
  
   @Override
   public void load() {
  
   }

   // And if we want to implement connect/disconnect methods, do it as following:-

  
    @Override
    public void connect() {
    super.connect();   # A call to super(parent) class which is DataSource class's connect method
    }

@Override
   public void disconnect() {
    super.disconnect();
   }

}

-------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

explain the principle of the inheritance give an example( CS related) of class inheritance
explain the principle of the inheritance give an example( CS related) of class inheritance
Consider the following class and the main method below. Trace the code, then answer the questions...
Consider the following class and the main method below. Trace the code, then answer the questions on the right. public class SomeClass { private String aName; private int aNumber; private boolean amAwesome; public SomeClass(String name, int number){ aName = name; aNumber = number; amAwesome = true; } public SomeClass(String name, int number, boolean awesome){ aName = name; aNumber = number; amAwesome = awesome; } public void methodAwesome(int number){ if(amAwesome) aNumber += number - 5; amAwesome = !amAwesome; } public int...
Explain how the genetic code is used to make protein.
Explain how the genetic code is used to make protein.
21. Consider the code below: [13] Employee class: class Employee { public: Employee(string theName, float thePayRate);...
21. Consider the code below: [13] Employee class: class Employee { public: Employee(string theName, float thePayRate); protected: string getName() const; float getPayRate() const; float pay(float hoursWorked) const; private: string name; float payRate; }; Definitions for some of the methods follow: Employee::Employee(string theName, float thePayRate) { name = theName; payRate = thePayRate; } float Employee::pay(float hoursWorked) const { return hoursWorked * payRate; } Manager Class: #include "employee.h" class Manager : public Employee { public: Manager(string theName, float thePayRate, bool isSalaried); protected:...
Explain why composition is generally preferred to inheritance for achieving code reuse.
Explain why composition is generally preferred to inheritance for achieving code reuse.
Explain why composition is generally preferred to inheritance for achieving code reuse.
Explain why composition is generally preferred to inheritance for achieving code reuse.
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
I need to see a working example using inheritance and the classes (below) The person class...
I need to see a working example using inheritance and the classes (below) The person class has first name last name age hometown a method called getInfo() that returns all attributes from person as a String The student class is a person with an id and a major and a GPA student has to call super passing the parameters for the super class constructor a method called getInfo() that returns all attributes from student as a String this methods has...
What output is produced by the following code?   Explain how it works. public class A {...
What output is produced by the following code?   Explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void main(String[]...
Differentiate between maternal inheritance and nuclear/general inheritance. Explain how transformation was discovered and how it relates...
Differentiate between maternal inheritance and nuclear/general inheritance. Explain how transformation was discovered and how it relates to the material of inheritance. Explain how bacteriophage were used to show that DNA was the material of inheritance. Explain how mutations are the toolbox of the geneticist and how mutations can be induced. Explain the tetranucleotide hypothesis and how it was refuted by Chargaff. Explain the numerical relationships between nucleotides in a DNA molecule.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT