Aggregation (HAS-A) in Java
- Aggregation represents part and whole relation between classes aggregation is a weaker relation in the way that in it either part or whole or both may have their independence existence.
- Student has address.
- Room has chairs.
- Room has fans.
- Composition repentance mode up of relation between classes it is stronger relation in the way that in its existence of both participants depends on each other for
- Room has walls.
- Room has roof.
Example of Aggregation
Note: - Aggregation is implemented by containing object of a class as a data member in another class.
public class PermanentAddress
public String city, state,country;
public long pincode;
PermanentAddress(String city, long pincode,
String state, String country)
public class StudentInfo
public StudentInfo(long enrolmentNo, String name,
public void show()
System.out.println("Enrolment No : "+ enrolmentNo);
System.out.println("Name : " + name);
System.out.println("Address of Student=======");
System.out.println("City : "+perAddress.city);
public static void main(String s)
PermanentAddress address1 =new PermanentAddress(
"Shakarpur",110092,"New Delhi", "India");
StudentInfo stdInfo =new StudentInfo(43600393,"Sunil"
Enrolment No : 43600393
Name : Sunil
Address of Student=======
City : Shakarpur
State: New Delhi
Download this example