Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How to Logically Break Down a System for Inheritance

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to Logically Break Down a System for Inheritance

    I am working on a Java program that takes in information on various locations (could be a continent, country, city or point of interest) and creates a database that can be searched through. Currently I have a Location object class that has the variables for continent name, population, area and then Continent, Country, City and POI objects that extend the Location class. I thought that this would be a viable structure for the program, however POI objects do not have a Continent associated with it or population.

    I have a couple questions about my design, any incite on the breakdown of the inheritance model is greatly appreciated.

    1) Is the Location class necessary(Does it improve the implementation?

    2) If it is, then should the class itself or the methods be abstract?

    3) If it isn't could you point me in a good direction of a logical breakdown of the system.

    Thank you in advance for any assistance provided.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to Logically Break Down a System for Inheritance

    You could create multiple layers of inheritance. Something like this:
    Location (abstract or interface) - contains: name, area
    POI (concrete, extends/implements Location) - contains any new stuff
    PopulatedLocation (abstract or interface, extends/implements Location) - contains: Continent_Name, population
    Continent (concrete, extends/implements PopulatedLocation) - contains any new stuff
    Country (concrete, extends/implements PopulatedLocation) - contains any new stuff
    City (concrete, extends/implements PopulatedLocation) - contains any new stuff

    So POI and PopulatedLocation inherit from Location. PopulatedLocation contains the stuff that POI doesn't care about. Then, Continent, Country, and City all inherit from PopulatedLocation. Since PopulatedLocation is-a Location, and Continent/Country/City is-a PopulatedLocation, Continent/Country/City are also Locations.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    JNeether (March 20th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to Logically Break Down a System for Inheritance

    Thanks aussie for the the reply. The problem I have is that I have the below Location class
    public class Location {
     
    	/**
    	 * The name of the location.
    	 */
    	private String name;
     
    	/**
    	 * The population of the location.
    	 */
    	private int population;
     
    	/**
    	 * The area of the location.
    	 */
    	private int area;
     
    	/**
    	 * A constructor for a Location object with all variables
    	 * empty.
    	 */
    	public Location(){
    		this.name = "";
    		this.population = 0;
    		this.area = 0;
    	}
     
    	/**
    	 * 
    	 * A constructor for a Location object.
    	 * 
    	 * @param 		name		The String to be assigned to the 
    	 * 							object's name.							
    	 * @param 		population	The int value to be assigned
    	 * 							as the object's population.
    	 * @param 		area		The int value to be assigned as 
    	 * 							the object's area.
    	 */
    	public Location(String name, int population, int area){
    		this.name = name;
    		this.population = population;
    		this.area = area;
    	}
     
    	/**
    	 * An accessor for the variable name.
    	 * 
    	 * @return Returns the name of the location.
    	 */
    	public String getName() {
    		return name;
    	}
     
    	/**
    	 * A mutator that sets the name of the location.
    	 * 
    	 * @param name	The String to be assigned to 
    	 * 				the Location's name.
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	/**
    	 * An accessor for the variable population.
    	 * 
    	 * @return Returns the population of the location.
    	 */
    	public int getPopulation(){
    		return this.population;
    	}
     
    	/**
    	 * 
    	 * @param population	The int to be assigned to 
    	 * 						the Location's population.
    	 */
    	public void setPopulation(int population){
    		this.population = population;
    	}
     
    	/**
    	 * An accessor for the variable area.
    	 * 
    	 * @return Returns the area of the location.
    	 */
    	public int getArea(){
    		return this.area;
    	}
     
    	/**
    	 * 
    	 * @param area	The int to be assigned to 
    	 * 				the Location's area.
    	 */
    	public void setArea(int area){
    		this.area = area;
    	}
     
    }

    By implementing the PopulatedLocation I get that the population issue is alleviated, however when I am storing the database I get lost. I will be using the LinkedHashMap data structure to store the data. Would having a LinkedHashMap<String countryName, Country country> for each Continent object. Then, a LinkedHashMap<String cityName, City city> for each Country object, as well as a POI list for each Country? Would that be efficient later on when having to sort and search, whether it be sorting the Country database specifically or searching for a specific element in each list?

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to Logically Break Down a System for Inheritance

    You could do something like that, but there are probably better options.
    When designing a data structure, you need to ask: how am I going to access the data?
    Should the data ALWAYS be sorted a specific way and will you ALWAYS search by the name? If yes to both of those, perhaps a SortedMap would be better than a LinkedHashMap.
    Will you be searching in ways other than just by name? If yes, use a List instead of a Map, because a Map would provide you with absolutely no improved performance if you search by anything other than the key (probably decreases performance even).
    Should all the counties, cities, ect. be stored together instead of in different Maps/Lists?

    In case you don't fully understand inheritance, you can create a generic list and add child objects into it. For example, an ArrayList<Location> would accept counties, cities, POIs, ect. all together, since they all inherit from Location.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to Logically Break Down a System for Inheritance

    I do get that I could create any kind of a Collection of type <Location>, however whether I should have individual collections for each type (Continent, Country, ... etc) or have a collection that has all types together. If I do the latter then I will have to do checks for the class type when sorting and then put the sorted objects, lets say all Country objects are being sorted alphabetically, in the beginning of the collection.

    [0] = Belize
    [1] = France
    [2] = Germany
    [3] = Romania
    [4] = United Kingdom
    [5] = North America
    [6] = Asia
    [7] = Europe
    [8] = South America

    My issue is that if the Germany object exists than the Europe object must exist because the database must be able to be sorted by continent or country. The program will read in a Continents.txt(Example input would be "Asia, 234223(area), 23434124(population)" , Countries.txt("Germany, 645(area), 2342234(population), Europe", Cities.txt and POI.txt file, therefor even if the Continents.txt file doesn't contain Europe, and the Countries.txt file has Germany the Europe Continent will be created. I am tempted to create all 7 Continent objects when the program starts but I know that this could create more information than the program needs. I believe that it will be easier to sort and search by the specific parameter by using a collection for each Continent, however I want to code the program correctly. Could you shed some light on this?

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to Logically Break Down a System for Inheritance

    Ok, I'm going to suggest a bit of a different take on this. We are going to use some creative data storage and referencing to reduce your need to do lookups and whatnot.
    Tell me if these guidelines make sense:
    Framework:
    1. Create a helper class. This helper class should contain:
    a) static Lists for each Location type (ie: one of continent, country, ect.)
    b) 3 static methods for each list: set, get, add
    2. Country should not contain a String name of a Continent, but rather a reference to a Continent object. Additionally, City should not contain a String name for Continent or Country, but references to Country and Continent objects.
    Logic:
    1. When a new item is read into the program, you will dynamically populate the lists in the helper class with any locations, including dependent ones. In order to do this, our helper methods need to have a bit of extra logic:
    a) the "get" method needs to find the corresponding Continent, Country, ect. in the list. If one does not exist, we will dynamically create a "templated" version, add it to the list, and return it. This ensures that the Country "Europe" will exist, even before we read it in.
    b) the "add" methods will add new Continents, Countries, Cities, ect. to the lists. But, before adding, it first needs to determine if a conflicting one already exists. If a conflicting one does exist, we need to overwrite the existing one with the new data we are reading in. These conflicting versions will be the "templates" mentioned in part a. We need to update the templated version to include all the data we are reading in.
    2. So, let's say the first thing we do is read in Germany. The logic would go like this:
    a) Create a new Country object with the name: Germany. While creating the new Country object, we need to lookup the Continent: Europe.
    b) Helper.getContinent("Europe"); Europe is not found in the Continent list, so we create a new Continent with the name: Europe (no population or area data yet), add it to the Continent list, and return the new Continent.
    c) We now have the Continent Europe, we add it to Germany's Continent variable, and we add Germany to the Country list
    d) Helper.addCountry(germany); Searches the list for an already existing Country named: Germany and doesn't find one, so the new country is just added to the Country List.
    3. Now let's read in Europe:
    a) Create a new Continent with the name: Europe. Add the Europe to the Continent list.
    b) Helper.addContinent(europe); Searches the list for an already existing Continent named: Europe and finds the templated one we created earlier for Germany. Instead of adding the new Continent, it instead copies the area and population values from our new Continent into the template.

    Germany's reference to Europe has not changed, but the area and population values in the Europe instance has been updated. So if Germany wanted to see Europe's area and population, it already has access to that without any additional lookups.

    Tell me how much of this makes sense. We are doing a dynamic lazy-load (which means: loading data on a need-to-have basis instead of arbitrarily). When we initially load Germany, it will not have all of Europe's information, but that is ok, because that information will update itself later when we load Europe's data in. Furthermore, we only need to look up the continents for each country once: when we load the country in. From then on, we have a reference to the country's continent, so we don't need to do any additional lookups later to find a country's continent.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to Logically Break Down a System for Inheritance

    It makes sense, however I have been encouraged to encapsulate the data fields. Therefor by making the Lists static it will not meet that criteria. I should mention I will eventually expand this project to utilize the MVC (Model View Controller) for the interface.

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to Logically Break Down a System for Inheritance

    Currently I have a Location object class that has the variables for continent name, population, area and then Continent, Country, City and POI objects that extend the Location class.
    Here's my take (I did not read through every word of the above posts so I hope it's not redundant): I see no need for inheritance (based upon the information given). Inheritance can be beneficial in cases where behavior should be implemented/altered - which I don't see in this case, rather it seems you wish to classify Locations (correct me if I'm wrong). If this is the case, then specify it's type by, say, adding a method which returns an Enum type specifying type (Continent, Country, etc...). This can also allow you to Map type to a Collection of Locations so you can search by type (if you so desire). You can also, if necessary, organize the data intro a Tree/Graph - in which each Location has one or more parents and children - the parent being its containing Location and children Locations it contains - this more complicated step may not be necessary depending upon how you wish to access the data.

    My advice going forward would be to write down what methods are required for accessing the data (I'm still unclear at this point what that may be so I can't be specific) - this is the contract (aka Interface) which allows clients access to the data. Say, for example, it might have a method like 'searchContinentsByName(String name)'. Doing so can help define both what a client can access, as well as help you define how you wish to organize and implement 'under the hood'.

  10. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to Logically Break Down a System for Inheritance

    It makes sense, however I have been encouraged to encapsulate the data fields. Therefor by making the Lists static it will not meet that criteria. I should mention I will eventually expand this project to utilize the MVC (Model View Controller) for the interface.
    It shouldn't violate that. When you load the data in, you have to load it into somewhere. What I was suggesting was a single central data core which can be used to lookup values.
    Perhaps it would make more sense if I just showed you:
    [highlight=java]
    public class Helper {
    private static List<Continent> continentList = new ArrayList<>();
    private static List<Country> countryList = new ArrayList<>();
    private static List<City> cityList = new ArrayList<>();
    private static List<POI> poiList = new ArrayList<>();

    public static List<Continent> getContinentList() {
    return continentList;
    }
    public static void addContinent(Continent toAdd) {
    // check list before adding, and only add if it already doesn't exist
    }
    public static Continent getContinent(String name) {
    // find the continent with the requested name and return it, create a template if it doesn't exist
    }
    public static List<Country> getCountryList() {
    %0
    Last edited by aussiemcgr; March 22nd, 2014 at 06:36 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. The Following User Says Thank You to aussiemcgr For This Useful Post:

    JNeether (March 24th, 2014)

  12. #10
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to Logically Break Down a System for Inheritance

    Apologies on the time to respond.

    I see what you are saying now, this should be very useful! I will let you know how the end product looks!

Similar Threads

  1. How do I make a break in this?
    By JavaA123Chris in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 28th, 2013, 06:10 PM
  2. How to create file in Logically(RAM) using java code?
    By msekharmca in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2013, 03:08 AM
  3. Code compiles, Is it logically correct?
    By Eriosblood in forum Object Oriented Programming
    Replies: 1
    Last Post: September 23rd, 2012, 11:34 PM
  4. Not sure how to logically correct a method.
    By orbin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 26th, 2012, 08:47 PM
  5. Break it down
    By [Kyle] in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2009, 09:04 PM

Tags for this Thread