JAVA - composition/aggregation confusion

In summary, you are having difficulty calling a method in the car class from the wheel class. You need to find out about the "protected" keyword and the "packages" keyword in Java.
  • #1
lavster
217
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
  • #2
Could you post the code (in a code block) that you're having difficulty with?
 
  • #3
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
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 :)
 
  • #5


First of all, don't worry if you are feeling confused - it's completely normal when starting out as a beginner in any subject! Understanding the differences between composition and aggregation can be tricky, but with some practice and guidance, you will eventually get the hang of it.

To clarify, composition and aggregation are both forms of association between classes in object-oriented programming. Composition is a strong form of association, where one class (the "whole") contains another class (the "part"), and the part cannot exist without the whole. For example, in your example, a car contains wheels and cannot function without them. Aggregation, on the other hand, is a weaker form of association, where one class (the "whole") contains another class (the "part"), but the part can exist independently. For example, a university contains students, but the students can also exist outside of the university.

In your case, it seems like you are trying to use composition between the car and wheel classes, but you are running into issues because the allwheels list is defined in the wheel class and not accessible in the car class. To solve this problem, you can declare the allwheels list as a public attribute in the wheel class, which will allow the car class to access it. This does not change the composition relationship between the two classes.

Alternatively, you can create a method in the wheel class that returns the allwheels list, and then call that method in the car class to access the list. This is a better practice as it encapsulates the data and avoids direct access to class attributes.

In summary, you can use composition in your relationship between the car and wheel classes without changing it to aggregation. It's important to understand the differences between the two and choose the appropriate one for your specific scenario. Keep practicing and don't hesitate to ask for help when needed. Good luck!
 

Related to JAVA - composition/aggregation confusion

1. What is the difference between composition and aggregation in Java?

Composition and aggregation are both types of relationships between two classes in Java. Composition is a strong form of association where one class is composed of one or more instances of another class. Aggregation is a weaker form of association where one class contains a reference to another class, but the other class can exist independently. In composition, the lifetime of the composed object is tied to the lifetime of the composing object, while in aggregation, the two objects have independent lifetimes.

2. How do you implement composition in Java?

Composition in Java is implemented by creating an instance of one class within another class and using that instance to perform operations or access data. The composed object is often created in the constructor of the composing class and can be accessed through a getter method. The composed object should also have a "has-a" relationship with the composing class, meaning it is an essential part of the class and cannot exist without it.

3. Can you give an example of composition in Java?

One example of composition in Java is a Car class composed of an Engine class. The Car class contains an instance of the Engine class and uses it to power the car. The Car class cannot exist without the Engine class, and any changes to the Car class will affect the Engine class as well.

4. How is aggregation different from composition in terms of code implementation?

Aggregation in Java is implemented by creating an instance of one class within another class, but the composed object is not created in the constructor. Instead, it is passed in as a parameter or set using a setter method. This allows the composed object to be changed or removed without affecting the composing object. Additionally, the composed object does not have a "has-a" relationship with the composing class.

5. When should I use composition and when should I use aggregation in Java?

Composition and aggregation should be used based on the nature of the relationship between two classes. Use composition when one class is a fundamental part of the other class and cannot exist without it. Use aggregation when the two classes have an independent relationship and can exist without each other. It is also important to consider the lifetime of the objects and how changes to one class will affect the other.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top