Sunday, July 8, 2007

Aggregation vs Composition


In normal terms, they both refer to member object but the survival or existence of the member object without the containing class or object or after the lifetime of the containing class or object makes the difference. Aggregation is also known as a 'has a' relationship because the containing object has a member object and the member object can survive or exist without the enclosing or containing class or can have a meaning after the lifetime of the enclosing object also.
Example ('has a'): Room has a table and the table can exist without the room. The table can have meaning without the room also.



Composition is also known as a 'is a part of' or 'is a' relationship because the member object is a part of the containing class and the member object cannot survive or exist outside the enclosing or containing class or doesnt have a meaning after the lifetime of the enclosing object. Composition is used over inheritance when different roles are to be played by a single entity. The 'is a' relation comes in this case.
Example 1 ('is a part of'): Computer Science Department is a part of the College. The Computer Science Department cannot exist without the college and the department has no meaning after the lifetime of the college.
Example 2 ('is a'): A Person is a supervisor. A Person is a husband. Supervisor and husband are the roles played by a single person. The husband role without the person has no meaning or cannot exist and similarly for the supervisor also.






From the above figure, Battery and Smart Phone explain Aggregation while the other relations explain Composition. The Smart Phone has a Battery. The Battery can exist without the Smart Phone and it has a meaning without the Smart Phone. On the other hand, the IMEI Number is a part of the Smart Phone and the existence of the IMEI number completely depends on the existence of the Smart Phone. The IMEI Number has no meaning without the Smart Phone. While the relation between IMEI Number and the Smart Phone explains the 'is a part of' type of composition, the other relation explains the 'is a' type of composition. The Smart Phone, in this case, plays roles at different times. The Smart Phone is a Camera. The Smart Phone is a Web Browser. The Smart Phone is a Music Player. The Music Player role of the Smart Phone cannot exist when the Smart Phone itself is not available.



Thus, Aggregation and Composition are almost the same conceptually but differ in implementation because, the contained or member object should be deleted in case of composition while the member object should not be deleted in case of aggregation.

48 comments:

Usman ur Rehman Ahmed said...

Please correct the sentence below,

"Thus, Aggregation and Composition are almost the same conceptually implementation wise because..."

Antony Vincent Pandian.S. said...

Thanks a lot for pointing the mistake. Have made the correction.

Aanshi said...

well, what is inheritence then???
If dog "is-an" animal than means class dog can be inherited from class animal.

Chris said...

Don't pay any attention to this document. It is really wrong and bad. "is part of" is not composition.

Antony Vincent Pandian.S. said...

@Chris,
good to see some opposition.

But put on your points on what is correct and why is this wrong so that everyone can know what is right and wrong. Just a statement that it is wrong wont do..

Daniel Fl├╝ck said...

Chris is right, this document is WRONG!

What is wrong:
"has-a" is both, aggregation and composition. The difference is not in this short two-word but in what you describe correctly about the livespan of an object.

"is-a" is always some form of inheritance, which is NEITHER an aggregation NOR a composition.

So please put this the right way.

Krishna said...

Daniel is correct,

IS-A is some form of inheritance.

SCJP 1.6 Mock Questions

Sanjeewa said...

i think The Smart Phone has(not is) a Camera. The Smart Phone has(not is) a Web Browser. The Smart Phone has(not is) a Music Player.

Offshore software development India said...

Nice post and very helpful for fresh programmer. Article describes the best tips for software development. Please continue writing....

Regards:-Offshore software development company

Yash said...

The Article is really good for understanding the different.

It has some fundamental issues as Daniel rightly pointed our.

Please take his comments and update this blog.

Great going... keep posting!!!

bhupesh said...

maar li sab ne Antony bhai ki to......
antony kindly correct it now...

@(BHANU)

Sharron Denice said...
This comment has been removed by the author.
Duc.Mediocre-Ninja said...

Totally agreed with Daniel et al.

+ Inheritance: "is-a" relationship

+ Composition: "has-a" relationship

+ Aggregation: "owns-a" relationship

Whilst both Composition and Aggregation implies an object "has another object" , the Aggregation has more dependency than Composition, usually that's the live-cycle/concurrent-update .

Duc.Mediocre-Ninja said...

Totally agreed with Daniel et al.

+ Inheritance: "is-a" relationship

+ Composition: "has-a" relationship

+ Aggregation: "owns-a" relationship

Whilst both Composition and Aggregation implies an object "has another object" , the Aggregation has more dependency than Composition, usually that's the live-cycle/concurrent-update .

Javi said...
This comment has been removed by the author.
Javi said...
This comment has been removed by the author.
Lalith said...

This is totally wrong.!!
Composition is 'has a' relation. That is the fundamental thing which was stated wrong.

Unknown said...

Composition and Aggregation are hotly debated topics and for the most part there isn't much of a consensus on how to define them.

Whether or not you can accurately describe them via has-a or is-a relationships I think for the most part we can agree that the lifetime analogy serves as a good distinction between the 2.

linodh thayyil said...

The last statement tells the story in brief by looking at composition and aggregation in terms of life span.

Composition is quite easy to understand if we see the class is responsible for creation and destruction of the other related entity.

Anyone who is not been able to destroy the related entity upon self destruction can bring Aggregation to our mind.

Ankit said...

a University can be seen as a composition of departments, whereas departments have an aggregation of professors.

VBAssassin said...

Litte confusing - i've always been told different to this:

"is a" = inheritance (such as dog is a animal)

"has a" = composition (such as phone has a screen or has a web browser)

"uses a" = aggregation (such as phone uses batteries)

John said...

"is a" is not composition. It represents an inheritance relationship i.e. subtyping.

Carlos Pineda said...

Is easy to understand when you think in terms of C++ language:

/* Composition */
class A
{
public:
class B
{
public:
int i;
}
x; /* "x" is a component of any instace of A */
};
void main()
{
A *a = new A();
a->x.i = 123;
delete(a); /* destroy the object referenced by "a" AND the component "x" */
}

/* Aggregation */
class B
{
public:
int i;
};
class A
{
public:
B *x; /* "x" is a reference to one object of type B */
};
void main()
{
A *a = new A();
B *b = new B();
a->x = b;
a->x->i = 0;
delete(a); /* destroy object referenced by "a" BUT NOT destroy the object referenced by "b" */
}

Carlos Pineda said...
This comment has been removed by the author.
Jacob Martin said...

This is really very very informative post thank you so much keep blogging..


Mr.S S Mishra

peterson said...

This is really nice. Thanks for sharing this informative article.

Qtp training

jose Faulkner said...

This is really nice. Thanks for sharing this informative article.
hadoop training

jack wilson said...

I have read your blog and i got a very useful and knowledgeable information from your blog.its really a very nice article. I did Loadrunner Course in Chennai. This is really useful for me. Suppose if anyone interested to learn Manual Testing Course in Chennai reach FITA academy located at Chennai Velachery.

dhanamlakshmi palu said...

This article is very interesting to learn.All the features are very helpful.Now i clearly know about joomla with the help of this article.
CCNA training in chennai | CCNA training chennai | CCNA course in chennai | CCNA course chennai

Pooja Doss said...

Oracle DBA Training in Chennai
Thanks for sharing this informative blog. I did Oracle DBA Certification in Greens Technology at Adyar. This is really useful for me to make a bright career..

Pooja Doss said...

Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
Websphere Training in Chennai

Pooja Doss said...

Data warehousing Training in Chennai
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..

Pooja Doss said...

Selenium Training in Chennai
Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

Pooja Doss said...

Oracle Training in chennai
Thanks for sharing such a great information..Its really nice and informative..

Pooja Doss said...

I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job.
SAP Training in Chennai

Pooja Doss said...

This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
Android Training In Chennai In Chennai

Pooja Doss said...

Pretty article! I found some useful information in your blog, it was awesome to read,thanks for sharing this great content to my vision, keep sharing..
Unix Training In Chennai

Pooja Doss said...

I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
SalesForce Training in Chennai

Pooja Doss said...

There are lots of information about latest technology and how to get trained in them, like Best Hadoop Training In Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies Hadoop Training in Chennai By the way you are running a great blog. Thanks for sharing this blogs..

Dinju Thomas said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
Selenium Training in Chennai | QTP Training in Chennai

Dinju Thomas said...

Thanks for Information Oracle Apps Technical is a collection of a bunch of collected applications like accounts payables, purchasing, inventory, accounts receivables, human resources, order management, general ledger and fixed assets, etc which have its own functionality for serving the business
Oracle Apps Training In Chennai

Dinju Thomas said...

Oracle Training in chennai | Oracle D2K Training In chennai
This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

PLC Training In Chennai said...

PLC Training in Chennai
PLC Training Institute in Chennai
PLC Training Center in Chennai
PLC Training Centre in Chennai
PLC SCADA Training in Chennai
SCADA Training in Chennai
Best PLC Training in Chennai
Best PLC Training Institute in Chennai
DCS Training in Chennai
Automation Training in Chennai
Automation Training Institute in Chennai
Process Automation Training in Chennai
Industrial Automation Training in Chennai
HMI Training in Chennai
VFD Training in Chennai
Inplant Training in Chennai
Embedded Training in Chennai
VLSI Training in Chennai
IT Training in Chennai
AutoCad Training in Chennai
Web Designing Training in Chennai

Diya Patel said...

Best SQL Query Tuning Training Center In Chennai This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

adams chitti said...

Hey nice work! I really appreciate your article and your effort made.. Technology Company in Chennai

adams chitti said...

In a Digital world, first impressions are vital and an outstanding Software Design is the key to success. Let our team help your site make an outstanding first impression with speed and accessibility: Technology Company in India

Peacock india said...

I have visited this blog for the first time. but really very good information. thanks for sharing all the good stuff. Technology Company in India

Peacock india said...

Thanks for Sharing this great article..Its really nice and useful for us… keep sharing.. Technology Company in India