Aggregation in Java
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
Has-a relationship is one in which an object of one class is created as a data member in another class.
It represents Has-A relationship.
Aggregation is a unidirectional association i.e. a one way relationship. For example, Department can have Students but vice versa is not possible and thus unidirectional in nature.
In Aggregation, both the entries can survive individually which means ending one entity will not effect the other entity
Why use Aggregation?
Code reuse is best achieved by aggregation.
Example of Aggregation
Java program to illustrate the concept of Aggregation.
Create Student Class.
int id ;
Student(String name, int id, String dept)
this.name = name;
this.id = id;
this.dept = dept;
Create Department class.
Student s; // Aggregation
public void printDetail()
System.out.println(s.name +" : "+s.id+" : "+s.dept);
Call these classes in main method
public static void main(String args)
Student s=new Student("John",23,"IT");
Department d=new Department(s);
In above example Department has Student .This is HAS-A relationship.
What next ?
super keyword in java