Why we need Aggregation?

Why we need Aggregation?

Aggregation is a special form of association. It is also a relationship between two classes like association, however its a directional association, which means it is strictly a one way association. It represents a Has-A relationship.

Aggregation is a relationship between two classes that is best described as a "has-a" and "whole/part" relationship.

example:

imagine a Student class that stores information about individual students at a school. Now let's say there is a Subject class that holds the details about a particular subject (e.g., maths,history, geography). If the Student class is defined to contain a Subject object then it can be said that the Student object has-a Subject object. The Subject object also makes up part-of the Student object, after all there is no student without a subject to study. The Student object is therefore the owner of the Subject object.
 

public class Subject{

private String name;

public void setName(String name)

{ this.name = name;

} public String getName()

{

return name; }

} public class Student

{ private Subject[] studyAreas = new Subject[10];

//the rest of the Student class }();

} }


  

Read More →