Image Image Image Image Image Image Image Image Image Image

Mudassir Shahzad | June 23, 2017

Scroll to top

Top

No Comments

Composition vs Aggregation – Explained simply

Composition vs Aggregation – Explained simply
Mudassir Shahzad

Often, I here about an interview question being asked that is “What is composition and aggregation ?” Or “What is the difference between composition and aggregation ?”

And trust me if you don’t know already, a lot of people (sometimes experienced developers) get stuck on this question.

Both, Composition and Aggregation, are actually kinds of Association.

 

Association – A teasing word, I know šŸ˜‰

As shown from its meaning,Ā Association isĀ a connection between any two or more things. We will not go into the detail of association, Just know that it is

 

simply a connection between two or more things

 

So repeat with me,

 

Association is a connection between two or more things.

 

Once more, with feeling…
Association is a connection between two or more things.

 

Remember it, I am going to bring it forward in the coming lines.

 

Now, let me give you the definition of composition and aggregation in two simple lines.

 

Composition : A connection or link between two objects where one object CANNOT exist without the other.

 

Aggregation : A connection or link between two objects where one object CAN exist without the other.

 

Example 1 – A clear example

 

Taken, a university has 1 to 20 different departments and each department has 1 to 5 professors. There is a composition link between a University and its’ departments. There is an aggregation link between a department and its’ professors.

 

Composition is just a STRONG aggregation, if the university is destroyed then the departments should also be destroyed.

 

But we shouldn’t kill the professors even if their respectives departments disappear.

 

A picture is worth a thousand words

university department mudassirshahzad.com

A university’s department

university professor mudassirshahzad.com

University Professor

 

And the relationship is

composition vs aggregation _ mudassirshahzad.com

 

Code Example

 

 

 

Example 2

 

0521oped_keane.r (1) - Copy - Copy
0521oped_keane.r (1)

Composition : If a car CANNOT run without an engine, that is composition. The car is composed of engine. Engine is an internal part of the car. Engine cannot exist without the car.

Aggregation : If a car CAN run without an engine, that is aggregation. And believe me, there are cars which can run without an engine. So here, engine is not ALWAYS an internal part of the car.

Code example

Composition

Aggregation

In the case of composition, the Engine is completely encapsulated by the Car. There is no way for the outside world to get a reference to the Engine. The Engine lives and dies with the car. With aggregation, the Car also performs its functions through an Engine, but the Engine is not always an internal part of the Car. Engines may be swapped, or even completely removed. Not only that, but the outside world can still have a reference to the Engine, and tinker with it regardless of whether it’s in the Car.

 

 

If this article helped you, return the help by sharing :-)

Related posts:

Submit a Comment

not found