Reputation: 4990
I find these two examples conceptually identical - yet one is a composition and the other aggregation.
In the first example, the relationship 'class (has-a) students' is a compositon.
A class contains students. A student cannot exist without a class. There exists composition between class and students.
In the second example, the relationship 'department (has-a) professors' is an aggregation.
If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist
In my opinion the first one is plain wrong. Notice that in the comment section of this SO question @TallPaul is questioning the first example as well. I think in practice it would delete all students enrolled in a class after each semester from the system. Moreover, the students would probably have to be created by the class on its initialisation, because composition in C++ is usually implemented as private attribute (not pointer). Am I right? Is there any way the first example makes sense?
Upvotes: 1
Views: 325
Reputation: 5585
Yes, those are weak examples, a Class must have a Subject would be a much better example for Composition. The relationship between a Class and Students is Aggregation because the life-time of the two is different.
See [UML Associations in Java] for more detailed examples1
Upvotes: 0
Reputation: 6987
There is no absolute truth and it all depends on the system you are modeling. You can create a system where students are instances that exist only in a specific class and when the class is deleted, so are the students. This may make sense when you don't want to store student information between classes for example.
Upvotes: 2