0

I have been assigned the task of re-creating the Oregon Trail through java. I have a few requirements - I must have a superclass - Location - and sublcasses - Trail, Landmark, City, Fort, and River and I must use and instantiate them all. I am currently thinking that the best way to approach re-creating the game would be to have a for loop (each loop representing one day) and decrement miles to next landmark, execute random chances to be sick, decrement food rations, etc. If I do it this way, the only logical way that I see to differentiate from location to location would be to create an instance of each class for each stop in the game (so Independence would be a city, Kansas River would be a River) but my question is, is there a way for me to put all of these instances into one object array even though they are different classes (from the same superclass)? I haven't been able to find any info on the syntax to do this.

3
  • sure, you can, make them implement same interface or declare an array of type Object[] Commented Mar 27, 2017 at 16:09
  • I think you are looking for Tuples github.com/javatuples/javatuples Commented Mar 27, 2017 at 16:09
  • Location[], Location[], Location[] Commented Mar 27, 2017 at 16:12

2 Answers 2

2

Of course you can do that in Java.

As all of your classes are inheriting from Location class. You can simply create array like this.

Location[] all = new Location[] {
  new Trail(),
  new LandMark(),
  new City()
};
Sign up to request clarification or add additional context in comments.

4 Comments

Ok. And I can even do multiple instantiations of each subclass? thanks so much for the quick reply!
Yes! please mark it as answer if it what you need [its the tick mark in the left side of answer]
if I were to do the following: 'Location[] all = new Location[] { LandMark chimneyRock = new Landmark(), etc... } would that be acceptable syntax?
LandMark chimneyRock = new Landmark(); Location[] all = new Location[] { chimneyRock }; you need to add instance of the class which is chimneyRock in this case.
0

Yes you can. What you are tying to do is called abstraction, and might be the most powerful feature of polymorphism. The is-a relationship created by extending the allows to allow us to program for the future, without knowing what the future will bring.

In your case you can create a new Location[] this will instantiate a new array of type Location, which will store references to Location objects. By definition, since a Trail is-a Location, you will be able to store references to Trail objects in a Location array. The same applies to all other subclasses of Location.

This explains Hari Lamichhane:

Location[] all = new Location[] {    
new Trail(),
new LandMark(),
new City()
};

However, the reference to that new Trail() object is actually stored as a Location reference (that's why you can store that reference in that new Location[]. Since Java is type safe, without safety casting that reference to a Trail reference you will not be able to access any methods not implemented in the super class (Location).

Lastly, if the implementation of each type of location is radically different one from the next you may want to consider a interface or abstract class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.