aspose file tools*
The moose likes Beginning Java and the fly likes Association Vs Aggregation Vs Composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Association Vs Aggregation Vs Composition" Watch "Association Vs Aggregation Vs Composition" New topic
Author

Association Vs Aggregation Vs Composition

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, I would request you to explain me the difference between Association Vs Aggregation Vs Composition. I have gone through several threads but unable to conclude anything at the end. Let us make it "THIS IS THE THREAD for Association Vs Aggregation Vs Composition". Please use the following code as base while showing the differences
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The three concepts are closely related, indeed. Aggregation and Composition are actually types of Association.

Association is an 'Has-A' relationship. In Java you can think of any time a class has an member of of a different type, then there is an Association between them. So for example:


In this case, the Person Has-A name and Has-A Costume, so there is an Association between Person and Name, and Person and Costume.

Aggregation and Composition are specializations of the Association relationship - they are both Has-A relationships, but the difference is the length of that relationship. In the above example, a Person probably has a single Name for the live of the Person, while the Person may have different Costumes depending on the situation. If this is the case then the relationship between Person and Name is Composition, and the relationship between Person and Costume is Aggregation.

Composition is an Association where the containing Object is responsible for the lifetime of the contained Object. To show this in Java Person might have a constructor like this:

Typically there would be no 'setName(Name)' methods. The Person is responsible for creating the Name, and when the Person is destroyed then the Name should (usually) be as well.

Aggregation is an Association relationship where the Association can be considered the containing class 'Owning' the contained class, and the lifetime of that relationship is not defined. 'Owning' can be determined as a single-direction Association. In the above example, the Person Has-A Costume, but the Costume would not Have-A Person. More importantly the Person to Costume Association is transient. In Java you can usually tell this because there are setter methods, or other means of adding / replacing the contained Object. So for the Person class you might have:


Does that make sense?


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Saibabaa Pragada wrote:Please use the following code as base while showing the differences


Sorry, posted my code before you edited yours with this. Hopefully you can apply the examples to your code
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Thank you Steve. I wish I have patience like you to write this kind of long explanation. This is definitely helpful for everybody. Small notes along with above is

Both Composition and Aggregation are Associations.
Composition IS-A Association.
Aggregation IS-A Association.

Composition is a strong association.
Aggregation is a weak association.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
Also see http://www.coderanch.com/how-to/java/AssociationVsAggregationVsComposition
Ashokkumar Thirunavukkarasu
Greenhorn

Joined: May 18, 2011
Posts: 1
Well explained. thanks steve.
thanks sai for the interview POV notes on the topic
Rajath Agasthya
Greenhorn

Joined: Aug 12, 2012
Posts: 1
Steve Luke wrote:

Aggregation is an Association relationship where the Association can be considered the containing class 'Owning' the contained class, and the lifetime of that relationship is not defined. 'Owning' can be determined as a single-direction Association. In the above example, the Person Has-A Costume, but the Costume would not Have-A Person. More importantly the Person to Costume Association is transient. In Java you can usually tell this because there are setter methods, or other means of adding / replacing the contained Object. So for the Person class you might have:


Great explanation, Steve! But in the above code where you illustrate Aggregation, you are not creating objects of currentClothes. Does that explain why the lifetime is undefined? If you create an object with 'new' operator, does it become a Composition?


Rajath Agasthya
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
Welcome to the Ranch, both of you

In this case the creation of a Costume object is left to code outside the Person classOf course, if you look in the dictionary, you find costume, clothes, garments and apparel all mean something very similar. You will also realise that the setClothes and getDressed methods actually do exactly the same thing. I am sure you would never design a class with two identical methods like that
Jp Rai
Greenhorn

Joined: May 27, 2011
Posts: 2
Association means Has-A relationship.
Inheritance means Is-A relationship.
Both Composition and Aggregation are Associations.
Composition ->Strong Has-A relationship
Aggregation -> Weak Has-A relationship.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Association Vs Aggregation Vs Composition