What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 30793 |  Welcome, Guest!   Register  Login
Home > Articles > OOPS > Composition Vs Aggregation

Composition Vs Aggregation

1 vote(s)
Rating: 5 out of 5
Article posted by Sudhakarj21 on 10/5/2009 | Views: 16011 | Category: OOPS | Level: Beginner red flag


Composition and Aggregation are the most confusing terms in OOPS. These 2 specifies relations between classes.

Introduction

Composition and Aggregation is most commonly used relations in class diagram. But many people gets confused with these 2 and some times even confused to implement the relations. The most simple thing about these is about the relations and life time of the object.
Composition

Lets take a simple example of Cycle. Cycle should contain 2 tyres and if we convert it to logical model below is the UML diagram




The above UML describes that Cycle contains 2 Tyres and the life time of each Tyre is maintained by Cycle. It is like "Tyre is Part Of Cycle". Let us see how it can be implemented in C#.

public class Tyre
{
. . .
}

public class Cycle

{

Tyre[] tires = new Tyre[]{new Tyre(), new Tyre()};

.......

}
Aggregation

On the other hand aggregation relation just represents reference between classes. For example consider Employee and contact information. Blow diagram represents the UML



The above UML describes Aggregation relation which mentions that Employee refers to Address. And life time of Address is not managed by Employee. It is like "Employee has a Address". Let us see how it can be implemented in C#.

public class Address
{
. . .
}

public class Employee

{

private Address address;

public Employee(Address address)

{

this.address = address;

}

. . .

}

Conclusion

Composition and Aggregation are important part of classes designs but most of the OOPS concepts are rounded with fundamentals only which doesn't describes about these relations.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:7 year(s)
Home page:
Member since:Monday, October 05, 2009
Level:Bronze
Status: [Member]
Biography:
 Responses
Posted by: Vishvvas | Posted on: 20 Jul 2011 07:02:24 AM | Points: 25

Very good article. There are few apprehensions about example of composition, from the perspective of cycle manufacturer its aggregreations as he can manufacture and sell the tyres seprately but from the perspective of end user its a composition as without a tyre , he can't ride/ use the cycle. A good example could be A cirlce composed of Points., An "Order" composed of "Orderline". For aggregration, the examples could be Window"--- "Shapes"', "StudentClass", "StudentGroup" ---"Student". The example of Employee and Address looks like simple association than aggregration as Address doen't give rise to Employee rather you can have entity "EmployeeDetails'" and "EmployeeAddress" as an example of aggregration.
Hope this helps.

Posted by: Chiragkanzariya | Posted on: 22 Nov 2012 02:06:59 AM | Points: 25

Nice Article

You can also check Basic concept of OOP here http://chiragkanzariya.blogspot.in/2012/11/oops-concept-with-real-world-example.html

>> Write Response - Respond to this post and get points
Related Posts

In this article I'm describing 'Abstraction', one of the main concept of Object Oriented Programming.Abstraction comes after inheritance and polymorphism, so make yourself clear in the first two topics then do work on Abstraction.I have described both of the topics in my previous lectures. Best of luck! faizan_ahmed

This article will discuss about Polymorphism in object oriented programming.

An overview of the Basic Concepts of OOPS programming

these are the basic concepts of Object Oriented Programming. I wrote it for those who don't have basic concepts of oop, and for those who feels programming a big monster. I tried to make them understand as much easiest as i could in my blog.. Best of Luck

Basic Concepts of Object Oriented Programming : Polymorphism This is based on Polymorphism,in which i am trying to make Polymorphism as easy as i can for those who don't have confidence on their Concepts.. Regards : Software Engineer Faizan Ahmed

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 6:05:30 PM