aspose file tools
The moose likes Junior/Associate Certification (SCJA,OCAJ 5/6/8) and the fly likes 1.3Associations vs Compositions Big Moose Saloon
Register / Login
Bookmark "1.3Associations vs Compositions" Watch "1.3Associations vs Compositions" New topic

1.3Associations vs Compositions

Krikor Krumlian

Joined: Sep 24, 2004
Posts: 9
I found the following definitions on javaranch

An Association is a channel between classes through which messages can be send. As sending messages translates to calling methods in Java, Associations are typically (but not necessarily) implemented by references.
An Aggregation is an Association which denotes an "is part of" relationship. Unfortunately, the definition of this relationship is quite lax, so basically everyone is using his own interpretation. The only definitive (?) property is that in an instance graph, aggregations are not allowed to be circular - that is, an object can not be "a part of itself".

A Composition adds a lifetime responsibility to Aggregation. In a garbage collected language like Java it basically means that the whole has the responsibility of preventing the garbage collector to prematurely collect the part - for example by holding a reference to it. (In a language like C++, where you need to explicitely destroy objects, Composition is a much more important concept.) Only one whole at a time can have a composition relation ship to a part, but that relationship doesn't need to last for the whole lifetime of the objects - with other words, lifetime responsibility can be handed around.

Can anybody give me concrete java code examples on what the difference between the two are.
Composition in some books is described as a has-a relation ship but then what's association ?

Thank you
Mallesham Karnati
Ranch Hand

Joined: May 11, 2005
Posts: 40
Do you mean Inheritance Vs. Composition or Composition Vs. Aggregation? Because there can't be association vs. composition as far as I understand. In other words, association can be of two types; composition and aggregation.
Dave Wood
Ranch Hand

Joined: Aug 02, 2004
Posts: 161
1.3 Describe, compare, and contrast class compositions, and associations (including multiplicity: (one-to-one, one-to-many, and many-to-many), and association navigation.

Note that the objective does NOT mention aggregation. This was intentional given how little consistency there is in the definition of that distinction (aggregation vs. composition).

The key thing to understand about composition (for this exam) is that it implies a very strong association between the owning object and the owned object. Think "Car->Wheels" -- two cars can't share the same wheel, and if the car goes away, so do the wheels.

In terms of implementation, both simple associations and compositions both typically make use of object references or collections of object's just a question of the semantics of the relationships.

Co-Author of <a href="" target="_blank" rel="nofollow">Java Swing</a><br />Co-Creator of <a href="" target="_blank" rel="nofollow">SCJP 5.0</a> and <a href="" target="_blank" rel="nofollow">SCJA</a> exams
I agree. Here's the link:
subject: 1.3Associations vs Compositions