Sunday 30 October 2016

Java Source World: Association, Aggregation, Composition

Association, Aggregation, Composition | Java Source World
                                                                  Javasourceworld                   A Java quick reference blog



Association, Aggregation, Composition




Association:



Association is a relationship where all objects have their own life cycle and there is no owner. 

For Example: Teacher and Student

Explanation:

A single Teacher object can have more Students or A single Student can associate with multiple Teachers.

But there is no ownership between these two objects and they have their won lifecycle. Both can be created and deleted independently.

Aggregation:


Aggregation is a specialized form of Association where all objects have their own lifecycle, but there is ownership and a child object cannot belong to another parent object.

For Example: Department and Teacher.

Explanation:

A single Teacher cannot belong to multiple Departments, but if we delete the department the teacher object will not be destroyed.

We can think of it is a "Has-A" relationship.


Composition:


Composition is a specialized form of Aggregation and we can call this as a "death" relationship. It is a strong type of Aggreation. Child object doesn't have their lifecycle and If parent object deletes all child objects will also be deleted. 

For Example: Bank and Accounts

Bank can contain multiple Accounts but there is no independent life of an Account and any Account can not belong to two different Banks. If we delete the Bank, the Account will automatically be deleted.



Now we will see the differences between Aggregation and Composition with similar examples.


Differences between Aggregation and Composition:



Aggregation:


Aggregation is an association in which one class belongs to a collection. This is part of a whole relationship wehere a part can exist without a whole.

For example:

A line item(list of products) is a whole and product is a part. If a line item is deleted then corresponding product need not be deleted. So aggregation has a weaker relationship.


Composition:


Composition is an association in which one class belongs to a collection. This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted.

For example:

An Order is a whole and line items are parts. If an order is deleted then all corresponding line items for that order should be deleted. So composition has a stronger relationship.


Which one to favor composition or Inheritance?  


As per java docs, the guide is that inheritance should be only used when subclass "Is-A" super class.

Don't use inheritance just to get code reuse. If there is no "Is-A" relationship then use composition for code reuse. Over use of implementation inheritance (using the 'extends' keyword) can break all the sublcass if the superclass is modified.

Don't use inheritance just to get polymorphism. If there is no 'Is-A' relationship and all you want is polymorphism then use interface with composition, which gives you code reuse.







                                                                  Javasourceworld                   A Java quick reference blog

No comments:

Post a Comment