Important Concepts of Aggregation, Association and Composition
- Inheritance
- Aggregation
- Association
- Composition
Scenario's :-
- ProjectLead is an employee of Software Company.
- ProjectLead uses a swipe card to enter in Software Company.
- ProjectLead has Developers who work under him.
- ProjectLead has the responsibility of ensuring that the project is successful.
- ProjectLead's appraisal will be judged based on project success.
First Scenario: - The IS A Relationship
ProjectLead is an employee of Software Company. It’s an inheritance relationship or parent child relationship. Here Employee is the parent class and ProjectLead is the derived class means ProjectLead is a type of employee. ProjectLead is inherited from the Employee class.
Second Scenario: - The USES A relationship: Association
ProjectLead uses a swipe card to enter in Software Company. Here Both are using each other but they are having their own object lifetime and they can exist without each other. The point to think here in this relationship is there is no single owner.
Association is a relationship where all object have their own lifecycle and there is no owner. For example like Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.
Third Scenario: - The HAS A relationship with Parent: Aggregation
ProjectLead has Developers who work under him. It seems same type of relationship like Association, but the difference here is that Project Lead is the owner of Developer object. In the meantime both objects survives independently.
But… the Project Lead object can have its own life time which is completely disconnected from the Developer object. From a different perspective, it means that if the ProjectLead object is deleted, the Developer object does not die.This relationship is termed as an “Aggregation” relationship.
Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. For example like Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.
Fourth Scenario: - The OWNS A relationship or The Death relationship: Composition
Project Lead has the responsibility of ensuring that the project is successful. Project Lead's appraisal will be judged based on project success. Project Lead and the project objects are dependent on each other.
Both the objects is having the same lifetime. In other words, if the Project Lead is not good then the project will not be successful and if the project is having issues then Project Lead will not get good appraisal.
This above relationship is termed as the composition relationship. In this relationship, both objects are heavily dependent on each other. Both the objects are having same lifetime in other words, if one goes for garbage collection the other also has to be garbage collected.
Composition is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. For example like relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.