What is aggregation?

Category: JavaWhat is aggregation?
1 Answers
Editor">Editor Staff answered 2 years ago

Aggregation can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns. Aggregation is best described as a has-a relationship.
For example, The aggregate class Employee having various fields such as age, name, and salary also contains an object of Address class having various fields such as Address-Line 1, City,
State, and pin-code. In other words, we can say that Employee (class) has an object of Address class. Consider the following example.
public class Address {
String city,state,country;

public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;

public class Emp {
int id;
String name;
Address address;

public Emp(int id, String name,Address address) {
this.id = id;
this.name = name;

void display(){
System.out.println(id+” “+name);
System.out.println(address.city+” “+address.state+” “+address.country);

public static void main(String[] args) {
Address address1=new Address(“gzb”,”UP”,”india”);
Address address2=new Address(“gno”,”UP”,”india”);

Emp e=new Emp(111,”varun”,address1);
Emp e2=new Emp(112,”arun”,address2);


111 varun
gzb UP india
112 arun
gno UP india