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.

94 comments:

Anonymous 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!!!

Anonymous 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

Anonymous 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

Lucky said...

Thank you so much for sharing... how to use lucky patcher

sri krishna kumar said...

I really enjoy it, to reading this
Informatica Training In Chennai | Hadoop Training In Chennai | Sap MM Training In Chennai

vijay antony said...

Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals



Best Java Training Institute Chennai

Careen joseph said...

I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
Best Hadoop Training Institute In chennai

amazon-web-services-training-institute-in-chennai

vijay said...

I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
Best selenium training Institute in chennai

shethal said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Best Aws training Institute in chennai

Anonymous said...


My friend Suggest me this blog and I can say the best blog.Thank you so much for this.

Jira training

Avigdor Liberman said...

Nice post. I be taught one thing tougher on different blogs everyday. It can at all times be stimulating to learn content from different writers and apply a bit something from their store. I’d desire to use some with the content material on my blog whether or not you don’t mind. Natually I’ll provide you with a hyperlink on your internet blog. Thanks for sharing. online casino games

amilu B said...

I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
python training in chennai | python training in bangalore

python online training | python training in pune

python training in chennai

simbu said...

You blog post is just completely quality and informative. Many new facts and information which I have not heard about before. Keep sharing more blog posts.

java training in chennai | java training in bangalore

java online training | java training in pune

java training in chennai | java training in bangalore

simbu said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
java training in annanagar | java training in chennai

java training in marathahalli | java training in btm layout

java training in rajaji nagar | java training in jayanagar

amilu B said...

Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
python training in chennai | python training in bangalore

python online training | python training in pune

python training in chennai | python training in bangalore

Nila shri said...

It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
Data Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore

Saro said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..

rpa training in Chennai

rpa training in anna nagar | rpa training in marathahalli

rpa training in btm | rpa training in kalyan nagar

rpa training in electronic city | rpa training in chennai

rpa online training | selenium training in training

nivatha said...

Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging

Data Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore

Mouni yoga said...

Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...
python training in chennai
python training in Bangalore

Mouni yoga said...

Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
python training in tambaram
python training in annanagar
python training in jayanagar

saran latha said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
DevOps online Training

bala D said...

Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.



Best Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies

Selenium Training in Bangalore | Best Selenium Training in Bangalore

AWS Training in Bangalore | Amazon Web Services Training in Bangalore

Sivanandhana Girish said...

Thanks for your contribution in sharing such a useful information. Waiting for your further updates.

Selenium Training in Chennai
Best selenium training in chennai
iOS Training in Chennai
Salesforce Training in Chennai
Salesforce Training

yuvanthi said...

Nice article. I was really impressed by seeing this article, it was very interesting and it is very useful for me.
Franchise Business in India
Education Franchise
Computer Education Franchise
Education Franchise India
Computer Center Franchise
Education Franchise Opportunities in India
Spoken English Franchise
Franchise in Education Sector

Prityyou said...

Awwsome informative blog ,Very good information thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
Airport Management Courses in Chennai | Airport Management Training in Chennai | Airline Courses in Chennai | Airport Courses in Chennai | Airline and Airport Management Courses in Chennai

Shadeep shree said...

The blog you have posted is more informative. Thanks for your information.
Android Training Institute in Coimbatore
Android Training and Placement in Coimbatore
Android Development Training in Coimbatore
Best Android Training Institute in Coimbatore
Advanced Android Training

pavithra dass said...

Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
Good discussion.
Hadoop Training in Chennai
Cloud computing Training
Cloud computing courses in Chennai
Cloud Training in Chennai
best institute for big data in chennai
big data classes in chennai

Vicky Ram said...

Excellent post! keep sharing such a post

Guest posting sites
Education

Aruna Ram said...

I have to thank for sharing this blog, it is really helpful and I learned a lot from your blog.
Data Science Training in Vadapalani
Data Science Training in Chennai
Data Science Course in Chennai
Data Science Course in Vadapalani
Data Science Training in Nungambakkam
Data Science Course in Tnagar

nanadhini chandran said...

feeling so good to read your information's in the blog.
thanks for sharing your ideas with us and add more info.
Selenium Training in Ashok Nagar
Selenium Training in Nungambakkam
Selenium Training courses near me
Selenium Training in Chennai

nanadhini chandran said...

More informative,thanks for sharing with us.
this blog makes the readers more enjoyable.keep add more info on your page.
vmware Courses in T nagar
vmware Courses in T nagar
vmware Training in Saidapet

Anbarasan14 said...

This is a very helpful Blog for one who needs to learn in a short span of time.

TOEFL Training Institute in T-Nagar
TOEFL Coaching in T-Nagar
TOEFL Training Institute in Royapuram
TOEFL Coaching Classes near me
TOEFL Classes near Porur
TOEFL Training in Virugambakkam
TOEFL Classes in Moulivakkam
TOEFL Training in Kolapakkam

Sathish K said...

I would really like to thank you for posting a valuable information. It was very awesome to read.

Unix shell scripting Training in Chennai | Unix Certification Courses | Unix Certification in Chennai | Unix Certification in Adyar | Unix shell scripting Training in Velachery | Unix Training in Tambaram

Richa T said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read  about their market situation nowadays.
Java training in Bangalore |Java training in Rajaji nagar | Java training in Bangalore | Java training in Kalyan nagar

Java training in Bangalore | Java training in Kalyan nagar | Java training in Bangalore | Java training in Jaya nagar

shalinipriya said...

This is good site and nice point of view.I learnt lots of useful information.

Data Science Training in Chennai | Best Data science Training in Chennai | Data Science training in anna nagar | Data science training in Chennai

Data Science training in chennai | Best Data Science training in chennai | Data science training in Bangalore | Data Science training institute in Bangalore

Data Science training in marathahalli | Data Science training in Bangalore | Data Science training in btm layout | Data Science Training in Bangalore

johnsy sai said...

Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
Best Devops Training in pune==

Mouni yoga said...

Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career.
online Python training | python training in chennai | Data science training in bangalore

gkr ragini said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Best Devops Training in pune

Ezhil K S said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Data Science course in rajaji nagar | Data Science with Python course in chenni

Data Science course in electronic city | Data Science course in USA

Data science course in pune | Data Science Training institute in Pune | Data science course in kalyan nagar | Data Science Course in Bangalore

Mouni yoga said...

Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
online Python training | python training in chennai

Vignesh G said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training

gowthunan said...

Appreciating the persistence, you put into your blog and detailed information you provide.
nebosh course in chennai

Xplore IT Corp said...


Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
networking training
ccna Training

gowthunan said...

Write more; that’s all I have to say. It seems as though you relied on the video to make your point.
fire and safety course in chennai

Dwarakesh babu said...

Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.

Advanced AWS Amazon Web Services Interview Questions And Answers

Best AWS Tutorial |Learn Best Amazon Web Services Tutorials |Advanced AWS Tutorial For Beginners


Best AWS Online Training | No.1 Online AWS Certification Course - Gangboard

Best AWS Training in Toronto| Advanced Amazon Web Services Training in Toronto, Canada

sweety ganga said...

Really very nice blog information for this one and more technical skills are improve,i like that kind of post.

angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in chennai

automation anywhere online Training

angularjs interview questions and answers

LindaJasmine said...


Amazing Post. It shows your great understanding of the concept. Thanks for Sharing.
IELTS coaching in Chennai
IELTS Training in Chennai
IELTS coaching centre in Chennai
Best IELTS coaching in Chennai
IELTS classes in Chennai
Best IELTS coaching centres in Chennai
IELTS Centre in Chennai
IELTS Training
IELTS Course in Chennai

venu bharath said...

Very informative post. Looking for this information for a long time. Thanks for Sharing.
Tableau Training in Chennai
Tableau Course in Chennai
Tableau Certification in Chennai
Tableau Training Institutes in Chennai
Tableau Certification
Tableau Training
Tableau Course

sathya shri said...

The site was so nice, I found out about a lot of great things. I like the way you make your blog posts. Keep up the good work and may you gain success in the long run.
angularjs Training in bangalore

angularjs Training in chennai

automation anywhere online Training

angularjs interview questions and answers

aws Training in indira nagar | Aws course in indira Nagar