File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Difference b/w Association,Aggregation&Composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Difference b/w Association,Aggregation&Composition" Watch "Difference b/w Association,Aggregation&Composition" New topic

Difference b/w Association,Aggregation&Composition

Nakul Kasadwala

Joined: Oct 30, 2000
Posts: 22
Hello Everybody,
I am not able to differentiate b/w these 3 terms. Any one can please provide me with good link and example (preferably Java) and some explaination.
Thanks in advance
Adrian Ferreira
Ranch Hand

Joined: May 29, 2000
Posts: 118
Hello Nakul,
Association, aggregation and composition can be mapped using attribute or method calling relatioship, it deppend of your model. Only in conceptually perpective you can differentiate between them.
Talking about composition and aggregation:
"...the difficult thing is considering what the difference is between agregation and association." [Martin Fowler]
So, one example should not be used as a rule. I understand like this:
- Association: one class calls methods from other. But this relationship can also be mapped as an attibute.
- Agregation: one class is "part of" the other. It is like saying that a monitor is part of a computer. I always map this as an attribute. Here, more than one object may have a reference for the part object.
- Composition: a particularly aggregation where the part object can not exist without the wrapper class. Also, the part object can only exist in one object and not in two object. The difference is like value and reference object concept [Fowler, page 93].
In Java, objects is passed by reference, and I would like to know how people map composition relationship. I would suggest to use part objects like attributes but not in more than one outer object. If we create objects only inside outer class and do not use its reference outside as other attribute, I understand it like a composite object.
Comments, please.
David Kane
Ranch Hand

Joined: Sep 20, 2001
Posts: 65
Robert Martin has a pretty good explanation of this at (although the code examples are in C++).
The interesting thing to note is that there isn't a precise mapping of Java syntax to these relationships. E.g. Any of the three could be represented (at least in part) with an instance variable.
(paraphrasing Martin into Java terminology) an association can be implemented through an instance variable, although it might also be implemented as a method argument, or the creation of a local variable.
Adrian in the prior post commented that he typically implements aggregations as an attribute (instance variable), but Martin also says that you need to avoid a cyclic relationship.
Adrian's suggestion of using an Inner Class for Composition is fairly common as well, but with some care, a Composition relationship could be implented with between a pair of regular classes as well.

David Kane<BR> <A HREF="" rel="nofollow"></A> <BR>Author of <A HREF="" TARGET=_blank rel="nofollow">Software Architecture: Organizational Principles and Patterns</A><BR><A HREF="" TARGET=_blank rel="nofollow"></A><BR><A HREF="" TARGET=_blank rel="nofollow"></A>
Deepak Mahbubani
Ranch Hand

Joined: May 12, 2001
Posts: 68
Originally posted by David Kane:
[B]Robert Martin has a pretty good explanation of this at (although the code examples are in C++).

THE URL asks for a password !
David Kane
Ranch Hand

Joined: Sep 20, 2001
Posts: 65
That's weird. The site has never asked me for one.
David Kane
Author of Software Architecture: Organizational Principles and Patterns
I agree. Here's the link:
subject: Difference b/w Association,Aggregation&Composition
It's not a secret anymore!