Association vs Aggregation vs Composition

In object oriented programming or in Unified Modelling, relationships between objects can be classified as association, aggregation, composition and inheritance. 

 

Association

Association is a relationship between two objects. Here objects might not be completely dependent on each other.

Example: Student and teacher, where multiple students can associate with single teacher and single student can associate with multiple teachers. Both can be added and removed independently. 

 

Aggregation

Aggregation is a strong form of association implying a part-whole hierarchy. Aggregation can be considered as a “has-a” relationship. The parent object contains (has) child objects, but the child object can also survive or exist without the enclosing class. 

Example: Room has a table, but the table can exist without the room.

 

Composition 

Composition is a strong form aggregation, where the part is inside exactly one whole. Composition is also known as a ‘is a part of’ relationship. The part may also be created and destroyed by the whole. The member object (part) cannot exist without the containing class. 

Example: A department is part of a college and it cannot exist or has no meaning after the lifetime of the college. Another example is rooms in a house, which cannot exist after the lifetime of the house.

 

Inheritance

Inheritance is also a form of association in UML where we form classes using classes that has already been defined. Inheritance can be considered as a “is-a” relationship. Inheritance is unidirectional. In OO, the concept of IS-A is based on class inheritance or interface implementation. 

Example: For example, House is a building, Car is a vehicle.

 

More Examples

Example 1

class Man extends Human { } - Man is a Human.

class Man implements Human { } - Man is a Human.

 

Example 2

class Man { private BestFriend dog; } - Man has a dog, who is a BestFriend.

class Man { private Dog bestFriend; } - Man has a best friend who is a Dog. 

 

Example 3

class Tech { WebService s; }

class JavaTech extends Tech { Spring s; }

Relationships in Example 3:

JavaTech has-a WebService

JavaTech has-a Spring

JavaTech is-a Tech

Tech has-a WebService

 

Task for you

Find out the notations in UML for each of the above.

Comments

Nice explanation  ............ :)

Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!