Java Aggregation

java-Logo
Java Tutorials
Java Environment
Environment Setup
Declaration & Assignments
Flow Control
Miscellaneous
Java OOPs

Aggregation can be said as a relation between two classes that is best described as a has-a and whole/part relationship. It is a more specialized version of the association relationship. Now, what is association? In Java, when you think of a class having that has a member of the different type, then there an association can be formed. In a Java class, where there lies an entity reference, it becomes aggression. Let us take a situation; Student object contains much information such as roll, name, email_add, etc. It contains one more object named rank, which contains its information such as subj_Name, marks, board_name, exam-type, etc. as given below.

class Student{
  int roll;
  String name;
  Rank ranking;
}

In this case, Student has an entity reference ranking, so the relationship is Student HAS-A ranking Aggregation is also used for code reusability. An aggregation is a form of association where the relation of Association can be considered the containing class 'owning' the contained class. The lifetime of that relationship cannot be defined. 'Owning' can be determined as a single-direction Association.

Why Programmers use Aggression in Java?

As told earlier, for code reusability. Let us consider a case where we have used two classes, student class, and another id class along with staffs and teacher; to maintain Student id, teachers' id, and staff id, programmers do not need to use the same code again and again. Just use the reference of id class while defining each of these classes. This can be written as:

Student Has-A id (Has-a relationship between student and id)

Staff Has-A id (Has-a relationship between staff and id)

teachers Has-A id (Has-a relationship between teachers and id)


Scroll Back to Top