1

I am creating an open world game, where each "place" is made up of "sub-places" For example, I have Farm.java class that is graphically represented by a matrix of sub-places : a house, a pond, a garden, and a field, and the user can "move" through this matrix and interact with the different places.

So, in a Farm object I want a sub-place matrix/array that contains a house object, a pond object, a garden object, and a field object (all different classes). I would like to do the following:

SubPlace[][] map = new SubPlace[1][1]

but I cannot do this because there is no actual SubPlace.java class, and I am not sure how to push these different objects into the same matrix.

I want the matrix to look like this:

map[0][0]= new House();
map[0][1]= new Pond();
map[1][0]= new Field();
map[1][1]= new Garden();

How can i do this when these are all different classes? Do they need to extend/inherit/or something from a common SubPlace class? Or is there another way to push all those objects in the matrix?

Mike James Johnson
  • 724
  • 2
  • 8
  • 29

5 Answers5

2

All java objects ultimately extend java.lang.Object, so in the worst case, you could always use an Object[][]. But this is unelegant. Like you proposed, the best way would be to extract a base class (or interface) like SubPlace that will contain the common methods you need all the map objects to perform (e.g., draw()).

Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

Use Inheritance -

class Place {}
class Farm extends Place {}
class Pond extends Place {}
class House extends Place{} etc...

Place[][] map = new Place[1][1];
map[0][0] = new Farm();
Raman Shrivastava
  • 2,923
  • 15
  • 26
1

I guess that what you are wanted to do is program an interface.

I'd like to refer to one of my favorite answers in StackOverFlow which explain what does it mean to program an Interface. The example used in there is probably one of the best I've ever read.

In your case, all the classes like Pond, Field, Garden would Implement an Interface SubPlace.

Community
  • 1
  • 1
Yayotrón
  • 1,759
  • 16
  • 27
1

There are two general approaches. You can either use Interfaces or Abstract Classes. Both have the aim to provide some contracts (mostly in form of methods) to the rest of the program. Each of your SubPlaces may have certain properties ,e.g. an Image to display, so you may want to define a public Image getImage() method.

When to use what? Speaking performance, abstract classes are slightly faster than interfaces, but this should not influence your decision. You may want to use an abstract class, if the parts you are using are of the same kind. For example, Ford and Porsche are both CarManifacturer. If, however, the parts are fundamentally different, you may want to use an interface instead. Printable is a good example: a Page may be printable, as well as an Image or a RCHelicopter (3D-Printer...).

On further note, you may want to use interfaces, if you already use inheritance (since Java does not allow multi-inheritance).

There are instances, where you actually can use both. Thread is a good example: To get a Thread to work you can let your class extends Thread and then start() an instance of this class. Another common possibility to start a thread (especially with the advent of Java 8 and lambdas) is to pass a Runnable as an argument to the Thread-constructor. The user has both options ans can choose which one to use.

In your case, I think an abstract class should do the job.

Community
  • 1
  • 1
Turing85
  • 18,217
  • 7
  • 33
  • 58
0

Yes you have to have an interface SubPlace and implement whatever places you want.I have provided a mock

interface SubPlace {

}

class House implements SubPlace {

}

class Pond implements SubPlace {

}

class Field implements SubPlace {

}

class Garden implements SubPlace {

}

public class GUI {

    public static void main(String[] args) {
        SubPlace[][] map = new SubPlace[1][1];
        map[0][0] = new House();
        map[0][1] = new Pond();
        map[1][0] = new Field();
        map[1][1] = new Garden();
    }
}
Madhan
  • 5,750
  • 4
  • 28
  • 61