| |
GeekInterview.com > Interview Questions > Concepts > OOPS
| Print | |
Question: Difference Between Aggregation and Composition
Answer: What is the difference between Aggregation and Composition? (Asked in Polaris Interview for Java Experienced People, on April 11, in Chennai) |
| June 06, 2008 06:35:18 |
#1 |
| NIDA REHMAN |
Member Since: June 2008 Total Comments: 1 |
RE: Difference Between Aggregation and Composition |
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. |
| |
Back To Question | |