JAVA - composition/aggregation confusion

  • Context: Java 
  • Thread starter Thread starter lavster
  • Start date Start date
  • Tags Tags
    Confusion Java
Click For Summary

Discussion Overview

The discussion revolves around the concepts of composition and aggregation in Java programming, particularly in the context of class relationships. The participant seeks clarification on how to manage class attributes and relationships while maintaining the intended design structure.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the distinction between aggregation and composition in their Java classes, specifically regarding a "car" class and a "wheel" class.
  • The participant describes an attempt to use a List object named "allwheels" in the "wheel" class within the "car" class, leading to errors due to visibility issues.
  • Another participant suggests using the "protected" keyword or packages to allow access between classes, referencing a similar concept in C++.
  • Concerns are raised about the design of the "wheel" class containing a list of all wheels, with a suggestion that the list should reside in the "car" class instead.
  • The original poster clarifies that their example is meant for understanding and that they are willing to share actual code for further assistance.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best design approach for the classes, with differing opinions on where the list of wheels should be defined and how to maintain the relationship type.

Contextual Notes

There are unresolved issues regarding class design and the implications of using different access modifiers in Java. The discussion reflects varying interpretations of composition and aggregation.

lavster
Messages
213
Reaction score
0
im a bit confused - please help - I am 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 don't work. now I am thinking (in my head - haven't 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 doesn't have an equal sign it is an aggregation and so i couldn't 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 :)
 
Technology news on Phys.org
Could you post the code (in a code block) that you're having difficulty with?
 
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.
 
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 can't 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 that's confusing. If so, ill insert my actual code.

Thanks :)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 41 ·
2
Replies
41
Views
5K
  • · Replies 18 ·
Replies
18
Views
9K