Aggregation in Java
Aggregation in Java can be defined as relationship between two classes in which one class contain reference of another class therefore aggregation represent HAS-A relationship. An aggregate class containing reference of another class is said to be owner of that class and aggregate class can have multiple class referenced and each such class is said to be part of aggregate class.
The ownership cannot be defined in aggregation when class A refers class B and in turn class B also refers class A i.e when there is cyclic references in an aggregation relationship.In such case ownership can be determined and the relationship is simply one of association.
Example of Aggregate class:
public class Subject
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
//An Aggregate class
public class Student
{
private Subject[] studyAreas = new Subject[10];
//the rest of the Student class
}
In above example,Student is an aggregate class and also the owner of subject class. Subject class is holding details of subject whereas Student class must have details of student and also contain Subject object thus Student object has-a Subject object.The Subject object is said to be part of the Student object, as there can be no student without a subject to study.
0 Comment(s)