Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

JAVA - composition/aggregation confusion

  1. Nov 4, 2011 #1
    im a bit confused - please help - im a complete beginner!

    I have several classes, within which are several attributes defined and declared.
    The assocation between some of these classes are aggregation, and between others is compostion.

    Take the example of person and car being and aggregation composition and car and wheels being a composition relation. In the wheel class i have a List object defined and instantiated as "allwheels". (ie private List allwheels = new ArrayList). This is basically a collection of all the wheels of car, which is already defined and declared in the wheel class as wheel_fl wheel_fr etc. In my car class i want to call in and use the allwheels (remember this is in the wheel class). However, i get errors as the car class does not know what allwheels is as its part of wheel class and not declared and defined in the car class. However, i need the method using allwheels in the car class and i need allwheels defined and instantiated in wheels class.

    How would i get round this problem??

    I have tried many things which dont work. now im thinking (in my head - havent a clue what it would do in a programming sense) the simplest would be to have a line at the top of my car class (ie in public class car {}) that says:

    private List allwheels;

    However, my relationship between the two classes NEEDS to be a composition. If i insert this line will it become an aggregation?? My naive understanding is that if the line has "=new" then its compostion and if it doesnt have an equal sign it is an aggregation and so i couldnt do this as it would become an aggregation? is this correct?? if so, could you give me any pointers as to how to call in allwheels but keeping it a composition relation?

    Thanks in advance :)
     
  2. jcsd
  3. Nov 5, 2011 #2

    Borg

    User Avatar
    Science Advisor
    Gold Member

    Could you post the code (in a code block) that you're having difficulty with?
     
  4. Nov 5, 2011 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    I think you need to find out about the "protected" keyword, and possibly also about "packages". That is Java mechanism to allow one class to access data in another class. The "friend" keyword in C++ has the same purpose.

    But a "wheel" class that contains the list all the wheels in a "car" seeems a strange design. Shouldn't the "wheel" class be about the properties of one wheel, and the list of the car's wheels be in the "car" class? In that case, your problem might not exist.
     
  5. Nov 5, 2011 #4
    This is just an example so that i can get my head round it an then apply it to my code so that i know i have understood it completely...

    The line in the code IN FUNCTION CAR CLASS would be:

    myClass1.setAllwheels(allwheels);

    where setAllwheels is a function defined in myClass1 (random class in this case which is defined but cant think what to call it here) and allwheels is a List object defined in wheels class which is basically a list of the wheel objects declared and defined in wheels class.

    Perhaps its my example thats confusing. If so, ill insert my actual code.

    Thanks :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: JAVA - composition/aggregation confusion
  1. Java interpreter (Replies: 8)

  2. Java Cylinders (Replies: 2)

Loading...