Lets say A, B are classes then what does this UML diagram mean?
(1) A contains B
Or
(2) B contains A
I'm confused as to which direction the aggregation holds.
Best Answer
In both cases the class closest to the diamond is the one that contains the class farthest from it. The relationship indicates either a single contained object or a collection of them. Usually it is indicated graphically with a legend similar to the one used in database ERM diagrams: "1..1", "1..m", etc.
The filled diamond means composition. The contained objects has little or no use outside the container and perhaps the container is incomplete without them (or is in an invalid/unstable state without them). For example, every iPhone has a touch screen panel (the transparent thing with a flat cable attached to the side). The touch screen panel separated from the iPhone is not of much use. You don't see people carrying in their pockets detached touch screen panels with the flat cable hanging out.
The empty diamond means aggregation. The contained object can exist separatelly from the container and is useful outside the container. The container can live without the contained object. A keychain is a composite object with a lot of keys attached to it, the keys, laser pointers and can openers can work by themselves without being attached to a keychain and neither the keychain or the tool (key, etc) is left in an unstable state. You can add and remove this items to the keychain but they can exist by themselves as functional tools. The keychain itself can be epmty, and you can't say it is broken because of that.
It looks like you've roughly got it right! A package in a UML class diagram does equate to a package in Java. The JVM/Java's APIs are so large that often its treated as one black box in these diagrams (or not shown at all).
Going forwards I'd worry less about the formal UML notation and more about 'does my colleague grok this'. Java class diagrams can get very complex beyond a certain size.
Mind the difference between aggregation and composition!
While in an aggregation instances of both associated classes may exist without a relation (e.g. a restaurant and a chair), for a composition the one can not exist without the other (e.g. a child without a parent).
In your case an aggregation seems more appropriate to me, and than it is no problem to model a aggregation between Department and Car AND Company and Car.
Besides that: such a situation is often modeled like Company <*>-- Department <>-- Car (where only Department stores the list of cars).
Best Answer
In both cases the class closest to the diamond is the one that contains the class farthest from it. The relationship indicates either a single contained object or a collection of them. Usually it is indicated graphically with a legend similar to the one used in database ERM diagrams: "1..1", "1..m", etc.
The filled diamond means composition. The contained objects has little or no use outside the container and perhaps the container is incomplete without them (or is in an invalid/unstable state without them). For example, every iPhone has a touch screen panel (the transparent thing with a flat cable attached to the side). The touch screen panel separated from the iPhone is not of much use. You don't see people carrying in their pockets detached touch screen panels with the flat cable hanging out.
The empty diamond means aggregation. The contained object can exist separatelly from the container and is useful outside the container. The container can live without the contained object. A keychain is a composite object with a lot of keys attached to it, the keys, laser pointers and can openers can work by themselves without being attached to a keychain and neither the keychain or the tool (key, etc) is left in an unstable state. You can add and remove this items to the keychain but they can exist by themselves as functional tools. The keychain itself can be epmty, and you can't say it is broken because of that.
See also this answer: https://softwareengineering.stackexchange.com/a/336764/61852