This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Aggregation means that an object "has" another object. For instance, a Name could be an aggregation of Strings. The Name will have references to zero or more Strings that together are part of the Name. Aggregation does not imply ownership. While the Name is made up out of these Strings, that doesn't stop the same String instances from being used elsewhere in the program. They are not part of the life cycle of Name. When the Name ceases to exist, the Strings still do.
Composition on the other hand, means that an object "owns" another object. An example could be a Car, which has a private reference to an Engine. The Car is told to move and it in turn delegates it to calls to the Engine. The outside world doesn't know or care about the engine. When the Car is wrecked and picked up by the garbage collector, the Engine goes with it. The Engine is part of the Car's life cycle.
Note that the difference between aggregation and composition is small. If we can open the bonnet and remove the Engine from the Car before it is trashed, so we can put it in another car, this automatically means that the Car isn't composed of an Engine, but it's an aggregation of an Engine.
Association simply means one object has a reference to another object. Aggregation and composition are both types of associations.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Joined: Jul 30, 2010
Thanks for the reply.. however I am already aware of these concepts and want to know how these concepts are implemented in java
I implied much of the implementation in my earlier post. Here is an example.
In the case of composition, the Engine is completely encapsulated by the Car. There is no way for the outside world to get a reference to the Engine. The Engine lives and dies with the car. With aggregation, the Car also performs its functions through an Engine, but the Engine is not always an internal part of the Car. Engines may be swapped, or even completely removed. Not only that, but the outside world can still have a reference to the Engine, and tinker with it regardless of whether it's in the Car.