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 ?
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.
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 references...it's just a question of the semantics of the relationships.
Co-Author of <a href="http://www.oreilly.com/catalog/jswing2" target="_blank" rel="nofollow">Java Swing</a><br />Co-Creator of <a href="http://www.sun.com/training/catalog/courses/CX-310-055.xml" target="_blank" rel="nofollow">SCJP 5.0</a> and <a href="http://www.sun.com/training/certification/java/associate_beta.xml" target="_blank" rel="nofollow">SCJA</a> exams