Java Aggregation - The Coding Shala
Home >> Learn Java >> Java Aggregation
Java Aggregation is a type of Association. It is a relationship between two classes like association. If a class has an entity reference, it is known as Aggregation. Aggregation is a directional association that means it is strictly a one-way association. Java Aggregation represents a HAS-A relationship.
Aggregation is a weak association. An association is said to be aggregation if both Objects can exist independently that means if one object is not there still another object can be used. For example a Bank and an Employee object. Bank has an employee. The Bank has multiple employees but an Employee can exist without a Bank also.
Another Example is a Class and a Student Object. Class and Student can exist independently.
Point to Remember: Code reuse is best achieved by aggregation.
Example of Aggregation in Java
The following example of Student and Address explains the Java Aggregation:Output:
In the above example, the Student has an address and Object address or Student object that can exist independently. Here we use Address object in Student class as a reference.
Other Posts You May Like