Difference Between Aggregation and Composition

What is the difference between Aggregation and Composition?
(Asked in Polaris Interview for Java Experienced People, on April 11, in Chennai)

Questions by khadarzone   answers by khadarzone

Showing Answers 1 - 12 of 12 Answers

Association is of different types. Importantnly of three types.

1. I know u as well as so many other people as well. Same for you. You also know so many people.This kind of relationship is depicted as simple Association in UML.

This is always characterised by "has a" relationship. Like I have your reference. You have mine reference. Both or none.

2. I am not complete without you. You make me complete. Example a music system consists of sound box, CD player, graphic equalizer etc.

So in other words we may say, a music system is aggregation of CD player, casette player, graphics equalizer, sound box etc.

Or music system object has aggregation relationship with each of the things mentioned above.

3. I am not complete without you. And you dont exist outside me.

Example could be a tree?

Branches, leaves, fruits dont exists outside it? If tree dies everything dies.

This is strongest form of association and called composition relationship.

Another examples could be relationship between a house and its rooms. Rooms dont exist outside a house? Same room cannot be shared across two houses? And all the rooms persishes if the house collapses.

Keeping it Simple,
Aggregation or Composition depends up on life time of the child object.
If the child object cannot exist beyond the lifetime of its parent then the relationship is composite(Strong relationship)
If the child object can exist beyond the lifetime of its parent, then the relationship is aggregtion.However, at a particular moment the child object can be controlled only by a single parent(ie. Unshared), and it is the responsibility of the parent object to transfer the control of the child object to some other instance while its destruction.

Aggregation:
In aggregation , one object is composed of multiple objects and every object is useless without the relationship. Suppose there is a car object, so car object is useless without the tyre or steering object, and steering object is useless without car. This type of relationship is called Aggregation.

Compostion:
One object is composed of several objects, in this type of relationship, every object has its own existance

  Was this answer useful?  Yes

beekal

  • May 25th, 2010
 

Composition gives a part of relationship e.g

Engine ----------------------- part of ------------------------------CAr

Destroy car then engine would be automatically destroyed along with car. In such case Use composition, (i.e. create engine object within car class)

Aggregation gives us has a relationship. e.g.

Person--------------------------- has---------------------address

Destroy a person,  then address shouldn't be destoyed. coz you can live in US and i too can live in US, i.e if i am removed from system then address US shouldn't be removed coz still you exist in the system and you still live in US.

Hence aggregation is preferred (i.e create address object and pass the object to the class as the class arguments)

Composition:- All forms of composition include a has-a relationship.
There is two type of composition : association and aggregation.

Aggregation :- complex object is composed of other objects. e.g TV


Association : - It is used when one object wants another object to perform a service for it. e.g Music System with seperate Reciver, CD Player, Speaker etc which are connected to it by patch cords.

(Ref : The Object-Oriented Thought Process by Matt Weisfeld)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions