Java: Aggregation and Composition (HAS-A)


Pre-requisite: Java: OOPS Concepts, Java: Class, Java: Object

If objects are having an association as “HAS-A” then there could be two types of relationship between objects:

  1. Aggregation
  2. Composition

Aggregation

Aggregation is a special type of association where objects have their independent life cycle but there is ownership. These owners and associate objects have the “HAS-A” relationship.

For Example, A person may associate with an Organization but he/she has an independent existence in the world.

Benefits of Aggregation

  • Aggregation provides reusability.

Note: For code reusability, if classes having IS-A relationship use Inheritance and for Has-A relationship use Aggregation.

Example of Aggregation

Here Employee class is associated with address class with Aggregation (HAS-A) relationship. Employee class contained an object of Address, this address object contains its own information like city, state, country, etc. Both of these objects are not strongly associate because if employee looses their jobs but still have the same address.

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 Employee {
	int id;
	String name;
	Address address;

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

	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("Noida", "UP", "India");
		Address address2 = new Address("Greater Noida", "UP", "India");

		Employee e = new Employee(101, "Saurabh", address1);
		Employee e2 = new Employee(102, "Gaurav", address2);

		e.display();
		e2.display();

	}
}

Composition

The composition is special type of aggregation where one object is strongly associated with another object and more restrictive. When the contained object in “HAS-A” and one object can not exist without the existence of others it’s the case of composition.

For Example:

  • Wheels in a car.
  • House has a room. Here the room can’t exist without the house.
  • An employee has a job. A job can’t exist without an employee.

Benefits of Composition

  • The composition provides reusability.
  • The composition can control the visibility of another object and reuse only when needed.

Examples of Compositions
Here Person and Job class having a composition relationship. In this case, Job class creates at runtime whenever a new person comes and we can change the salary from backend without change on client-side.

public class Person {
	//composition has-a relationship
    private Job job;

    public Person(){
        this.job=new Job();
        job.setSalary(1000L);
    }
    public long getSalary() {
        return job.getSalary();
    }
}
public class Job {
	private String role;
    private long salary;
    private int id;

    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public long getSalary() {
        return salary;
    }
    public void setSalary(long salary) {
        this.salary = salary;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

}
public class TestComposition {

	public static void main(String[] args) {
		Person person = new Person();
		long salary = person.getSalary();

	}

}

Examples of Compositions & Aggregation
Let’s have an example of a University, it’s department and professors associated with departments.

University & Department Association: It’s a composition association because without University will exist in any department.
Department and Professors Association: It’s aggregate association because both are associated but not having any dependency like if department close professor lose. Both are are an independent entities.

import java.util.List;

public class University {
	 private List departments;

     public void destroy(){
         //it's composition, when i destroy a university I also
		 //destroy the departments. they cant live without university instance
         if(departments!=null)
             for(Department d : departments) d.destroy();
         departments.clear();
         departments = null;
     }

}
public class Department {
	private List professors;
    private University university;

    Department(University univ){
        this.university = univ;
        //check here univ not null throw whatever depending on your needs
    }

    public void destroy(){
        //It's aggregation here, if we fire any professor then they
		//will come out of department but they can still keep living
        for(Professor p:professors)
            p.fire(this);
        professors.clear();
        professors = null;
    }

}
public class Professor {
	 private String name;
     private List attachedDepartments;

     public void destroy(){

     }

     public void fire(Department d){
         attachedDepartments.remove(d);
     }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s