JAVA - composition/aggregation confusion

  • Java
  • Thread starter lavster
  • Start date
  • #1
217
0

Main Question or Discussion Point

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 :)
 

Answers and Replies

  • #2
Borg
Science Advisor
Gold Member
1,871
2,271
Could you post the code (in a code block) that you're having difficulty with?
 
  • #3
AlephZero
Science Advisor
Homework Helper
6,994
291
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.
 
  • #4
217
0
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 :)
 

Related Threads on JAVA - composition/aggregation confusion

  • Last Post
Replies
9
Views
1K
  • Last Post
Replies
4
Views
2K
Replies
1
Views
1K
Replies
19
Views
2K
Replies
2
Views
10K
  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
4
Views
936
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
4
Views
2K
Top