Difference between Association, Composition and Aggregation in Java, UML and Object Oriented Program

In Object-oriented programming, one object is related to other to use functionality and service provided by that object. This relationship between two objects is known as the association in  object oriented general software design and depicted by an arrow in Unified Modelling language or UML. Both Composition and Aggregation are the form of association between two objects, but there is a subtle difference between composition and aggregation, which is also reflected by their UML notation. We refer association between two objects as Composition, when one class owns other class and other class can not meaningfully exist, when it's owner destroyed, for example, Human class is a composition of several body parts including Hand, Leg and Heart. When human object dies, all it's body part ceased to exist meaningfully, this is one example of Composition.

Programmers often confuse between Association, Composition and Aggregation in Object oriented design discussions, this confusion also makes the difference between Association, Composition and Aggregation one of the popular questions in Java Interviews, only after the difference between abstract class and interface .

Another example of Composition is Car and it's part e.g. engines, wheels etc. Individual parts of the car can not function when a car is destroyed.  While in the case of Aggregation, including object can exists without being part of the main object e.g. a Player which is part of a Team, can exist without a team and can become part of other teams as well.

Another example of Aggregation is Student in School class, when School closed, Student still exist and then can join another School or so.  In UML notation, a composition is denoted by a filled diamond, while aggregation is denoted by an empty diamond, which shows their obvious difference in terms of strength of the relationship.

The composition is stronger than Aggregation.  In Short, a relationship between two objects is referred as an association, and an association is known as composition when one object owns other while an association is known as aggregation when one object uses another object.

In this OOPS tutorial, we will see a couple of more examples to understand difference between Association, Composition and Aggregation better.


Read more: http://javarevisited.blogspot.com/2014/02/ifference-between-association-vs-composition-vs-aggregation.html#ixzz4ukaSBFpD

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986789&siteId=291194637