3

I'm relatively new to programming and for one of my classes I have to create a class hierarchy for the following things: Country, State, County, City, Boundary, and River.

How would I do this?

I think I understand that I cannot use extends because it implies that one thing IS-A different thing, is that the correct way to think about it? Also, I understand that there is a HAS-A relationship in Java but I do not know how to use it, do I just create separate class files? Or, are there any other ways to do this?

Thanks for any help!

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
  • @JonK, no. I need Countries to be able to have multiple States, States multiple Counties, etc. and I need to be able to get a list of each Country's States and so on for each object. How could I change my question to make this more clear? – user3558214 Apr 21 '14 at 22:43
  • 1
    I think this is asking for _both_ inheritance and composition. A `Country` _has_ `States` whereas a `River` _is a_ `Boundary`. – Boris the Spider Apr 21 '14 at 22:43
  • Do you know how to create a `Person` object that *has a* field `name` of type `String`? Then you know how to create a `Country` class that has a field `county` of type `County`. – Jeroen Vannevel Apr 21 '14 at 22:43
  • This question might be worth a look: http://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition – JonK Apr 21 '14 at 22:44

3 Answers3

0

Think about what things are similar.

Country, State, County, City are all geographic entities that can contain each other.

A River is a geographic entity that can cross human-defined boundaries.

A Boundry is something that helps to describe any of the above.

Country, State, County, City and River all have some properties in common. For example, they probably all have a name. They also all have a Boundry. That implies that they perhaps should share a common base class (maybe GeographicEntity).

Country, State, County, City have a hierarchy, and they are human-defined. You might consider a subclass of GeographicEntity, say PoliticalEntity, that has things like:

  • The parent PoliticalEntity that it belongs to
  • A list of zero or more entities that it contains (e.g. a Country contains States).

Note that things are not quite that simple in real life (there are e.g. cities that cross county or even state lines), but for learning those edge cases can likely be ignored.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

A Country has a State
A State has a County
A County has a City
A City can have a River

And I guess you could say they all have Boundaries. Depends on what you mean by a Boundary. To implement:

public class Country
{
    private State states[];
    private Boundary boundary;
}

public class State
{
    private County counties[];
    private Boundary boundary;
}

public class County
{
    private City cities[];
    private Boundary boundary;
}

public class City
{
    private River rivers[];
    private Boundary boundary;
}

public class Boundary{}

I suppose sometimes States are bounded by Rivers if you want to be technical:

public class River extends Boundary{}
John
  • 3,769
  • 6
  • 30
  • 49
  • This assumes that a `Country` can only have one `State`, one `State` can only have one `County` and so on... – JonK Apr 21 '14 at 22:46
  • I see some 1:n instead of 1:1 relations here (e.g. Country has multiple States) – qqilihq Apr 21 '14 at 22:47
  • What would be the boundary of a `River`? – Jeroen Vannevel Apr 21 '14 at 22:47
  • Oh god people I was being generic, do you want me to make a full technical example? – John Apr 21 '14 at 22:48
  • @JeroenVannevel You could say that the shore lines are the boundaries of the river if you want to be technical. I didn't know you guys were going to rip on me for being generic hahah. Ill try to be more specific. – John Apr 21 '14 at 22:50
0

you are correct that extends is a IS-A a thing. What you need is the HAS-A thing. This can be achieved class and member relation.

lets take an example of country, state and city and start from the bottom ie the smallest one. since city is a real world entity. Lets create a class for city

class City{
   //members of city class
}

we know that a state can have multiple cities, say three for example. now lets create ha HAS-A relationship

class State{
  City city1;
  City city2;
  City city3
}

Similary, a country can have many states and indepandent cities( such as union territories of India).

class Country{
  State state1;
  State state2;
  City city1;
}

Hope its now clear to you.

maxx777
  • 1,320
  • 1
  • 20
  • 37