Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
In Object-Oriented programming, an Object communicates to other Object to use functionality and services provided by that object. Composition and Aggregation are the two forms of association.
Neha is employee of Axis
It is a special form of Association where:
- It represents Has-A relationship.
- It is a unidirectional association i.e. a one way relationship. For example, department can have students but vice versa is not possible and thus unidirectional in nature.
- In Aggregation, both the entries can survive individually which means ending one entity will not effect the other entity
Total students in institute: 4
In this example, there is an Institute which has no. of departments like CSE, EE. Every department has no. of students. So, we make a Institute class which has a reference to Object or no. of Objects (i.e. List of Objects) of the Department class. That means Institute class is associated with Department class through its Object(s). And Department class has also a reference to Object or Objects (i.e. List of Objects) of Student class means it is associated with Student class through its Object(s).
It represents a Has-A relationship.
When do we use Aggregation ??
Code reuse is best achieved by aggregation.
Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.
- It represents part-of relationship.
- In composition, both the entities are dependent on each other.
- When there is a composition between two entities, the composed object cannot exist without the other entity.
Lets take example of Library.
Title : EffectiveJ Java and Author : Joshua Bloch Title : Thinking in Java and Author : Bruce Eckel Title : Java: The Complete Reference and Author : Herbert Schildt
In above example a library can have no. of books on same or different subjects. So, If Library gets destroyed then All books within that particular library will be destroyed. i.e. book can not exist without library. That’s why it is composition.
Aggregation vs Composition
- Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Bank and Employee, delete the Bank and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a Human
- Type of Relationship: Aggregation relation is “has-a” and composition is “part-of” relation.
- Type of association: Composition is a strong Association whereas Aggregation is a weak Association.
Engine of car has been started Car is moving
In case of aggregation, the Car also performs its functions through an Engine. but the Engine is not always an internal part of the Car. An engine can be swapped out or even can be removed from the car. That’ why we make The Engine type field non-final.
This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.Arrays.equals() in Java with Examples
- Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java
- Java.util.Collections.frequency() in Java with Examples