2. Disclaimer:This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
4. They are the relationships among classes in
java
– Association
– Aggregation
– Composition
5. Association
• It is a relationship between two objects.
• In other words, association defines the multiplicity
between objects.
• You may be aware of one-to-one, one-to-many,
many-to-one, many-to-many all these words define
an association between objects.
• Aggregation is a special form of association.
• Composition is a special form of aggregation.
6. Association represents a general binary relationship
that describes an activity between two classes
7. Another example for Association
• Let’s take an example of Teacher and Student.
Multiple students can associate with single
teacher and single student can associate with
multiple teachers but there is no ownership
between the objects and both have their own
lifecycle. Both can create and delete
independently.
8. Aggregation
• It is a special case of association.
• A directional association between objects.
• When an object ‘has-a’ another object, then
you have got an aggregation between them.
• Direction between them specified which
object contains the other object.
• It is also called ‘Has-a’ relationship.
9. Note:
If you delete the parent object, even then the
child object may exist. One object can contain
the other, but there is no restriction that the
composed object has to exist in order to
have existence of child object.
10. Examples for aggregation
• Take an example of Departments and developers. A developer
can be only in one department like dotnet, java etc. but if we
delete the Department object, still the Developer object
exists. This is a “Has- a” relation.
• Let’s take another example of Department and teacher. A
single teacher can not belongs to multiple departments, but if
we delete the department teacher object will not destroy.
11. Composition
• It is a special case of aggregation.
• In a more specific manner, a restricted
aggregation is called composition.
• When an object contains the other object, if
the contained object cannot exist without the
existence of container object, then it is called
composition.
12. Example for composition
• A class contains students. A student cannot
exist without a class. There exists composition
between class and students.
13. Difference between aggregation and
composition
• Composition is more restrictive.
• When there is a composition between two
objects, the composed object cannot exist
without the other object.
• This restriction is not there in aggregation.
Though one object can contain the other
object, there is no condition that the
composed object must exist.
14. • The existence of the composed object is
entirely optional.
• In both aggregation and composition,
direction is must. The direction specifies,
which object contains the other object.
15. Example:
A Library contains students and books.
Relationship between library and student is
aggregation.
Relationship between library and book is
composition.
– A student can exist without a library and therefore
it is aggregation.
– A book cannot exist without a library and
therefore its a composition.
16. Another Example
• The relation between a car and a tyre is an
aggregation because the tyre is still a tyre if it
is not attached to a car. The tyre has a life of
its own. It exists outside of a car and you can
use it on another car.
• The relation between a car and a carburetor is
a composition because the carburetor has no
use if it is out of a car.
17.
18. Video link:
You can refer more about this topic
https://www.youtube.com/watch?v=0po_wm
SEW1Q