If a class has an entity reference, it is known as Aggregation. Aggregation represents the HAS-A relationship.

Consider a situation, Employee object contains many pieces of information such as id, name, email I'd, etc. It contains one more object named address, which contains its own information such as city, state, country, zip code, etc. as given below.

class Employee{  
int id;  
String name;  
Address address;//Address is a class  
...  
}


In such case, Employee has an entity reference address, so the relationship is Employee HAS-A address.

Why use Aggregation?

  • For Code Reusability.

When use Aggregation?

  • Code reuse is also best achieved by aggregation when there is no is-a relationship.
  • Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.