As the Aggregation and Composition is related Association or we can say it gives the understanding of relationship between object or anything else.
I posted this question because i asked one question in Interview that what is the Composition and Aggregation.
So as per my understanding i given my idea which is as follow.
http://www.coderanch.com/t/522414/java/java/Association-Aggregation-Composition
Aggregation, Association and Composition
Association vs. Aggregation vs. Composition in Java
and also visited much more.
and my basic explanation was related that Aggregation indicates loose relationship while Composition indicates Strong relationship and clear explanation related to this.
But the Interviewer Insulted me and said this is the theoretical concept about which you saying i want the perfect Java code in which how they differ and also told if i will give you one small Application then how would you identify that this is the Aggregation and this is the Composition?
So now i want to understand pure Technical Concept and Java code in which how they differ and what it is?
A would like to add additional information to explained answers
Aggregation is a special form of Assosation (Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many)
Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.
- Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Department and Employee, delete the Department and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a Human.
- Type of Relationship: Aggregation relation is “has-a” and composition is “part-of” relation.
- Type of association: Composition is a strong Association whereas Aggregation is a weak Association.
Reference: https://www.geeksforgeeks.org/association-composition-aggregation-java/
Consider a simple example of a
LinkedList
class. ALinkedList
is made up ofNodes
. HereLinkedList
class is the owning object. When it is destroyed, all theNodes
contained in it are wiped off. If aNode
object is deleted from the list, theLinkedList
object will still exist.Composition is implemented such that an object contains another object.
This is composition.
Another example, consider a
Circle
class which has aPoint
class which is used to define the coordinates of its center. If theCircle
is deleted its center is deleted along with it.Example of composition from java collections api :
HashMap
has anEntry
class. If theHashMap
object is deleted all theEntry
objects contained in the map are deleted along with it.Aggregation is also a type of Object Composition but not as strong as Composition. It defines "HAS A" relationship. Put in very simple words if Class A has a reference of Class B and removing B doesn't affect the existence of A then it is aggregation. You must have used it at some point.