• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Association Vs Aggregation Vs Composition

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 162
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see https://coderanch.com/how-to/java/AssociationVsAggregationVsComposition
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well explained. thanks steve.
thanks sai for the interview POV notes on the topic
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to wake up an old thread. Some definitions of aggregation and composition talk about Parent-Child relationship. Is this parent-child concept the one based on inheritance where we inherit? Or do they mean that in the parent-child relation class A has a reference to class B? Or aggregation/composition have nothing to do with parent-child relationship and they should be called container and contained object?
 
raja singh kumar
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add,

If class A has a reference of class B means that class A is dependent on class B and this dependency is different from parent-child relationship(or inheritance), right?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raja singh kumar wrote:Sorry to wake up an old thread.

There is nothing wrong with waking old threads

Some definitions of aggregation and composition talk about Parent-Child relationship. Is this parent-child concept the one based on inheritance where we inherit? . . .

No. Any analogies to parent and child fall down very quickly when you try to apply them to object‑oriented programming, so I think you shou‍ld forget all about parent and child.

The terms parent and child are often used to refer to folders inside other folders, and GUI components inside other components, but it shou‍ld be obvious that is very different from parents and children in real life.
 
raja singh kumar
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, it would be better to say container and contained object when talking about association, aggregation and composition?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic