Understanding Aggregation in Java: Has-A Relationship

This is an extended article on Inheritance in Java which we discussed previously. We talked a lot about Inheritance which creates IS-A relationship between two classes or entities. In this article, we are going to discuss the HAS-A relationship achieved with Aggregation in Java. I shared a story in Inheritance article and correlated this OOPs concept with a real-time example. Similarly, we can correlate Aggregation as well with any real-life scenario, for example, Ramesh has a dog, Inviul has employee, Mukesh has two eyes, Jenny has a cat, etc. When we convert these examples as problem statement then we will use the concept of Aggregation. In a simple manner, we can say that a class gets an entity reference from another class which ultimately builds Aggregation or HAS-A relationship.

Let’s start the learning journey!

Aggregation in Java

Well, many of you would be thinking that Why is it required to use Aggregation when the IS-A relationship can handle it properly?

Let me answer your question. Inheritance will only prevail if it maintains the IS-A relationship throughout the lifecycle of the application. If there is no IS-A relationship then it’s good to go with the HAS-A relationship. It helps in code reusability and proper maintenance of the code.

Aggregation in java

Before we begin a problem statement I want to share links of some of the useful blog posts published here:

Problem Statement in Aggregation

We are creating a database of all the employees of the organization and there is an address attached to each employee. Hence, the employee has an address. Let’s convert this statement into a Java program which simply implements the concept of Aggregation.

package my.project;

public class Address {
	String city; 
	String state; 
	String country;  

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

}
package my.project;

public class Employee {
	int empId;  
	String empName;  
	Address empAddress;  

	public Employee(int empId, String empName, Address empAddress) {
		this.empId = empId;  
	    this.empName = empName;  
	    this.empAddress=empAddress;  
	}
	
	public void display(){  
		System.out.println(empId+" - "+empName);  
		System.out.println(empAddress.city+" "+empAddress.state+" "+empAddress.country);  
	}  

	public static void main(String[] args) {
		Address address1=new Address("Patna","Bihar","India");  
		Address address2=new Address("Mumbai","Maharashtra","India");  
		  
		Employee emp1=new Employee(1,"Avinash",address1);  
		Employee emp2=new Employee(2,"Mishra",address2);  
		      
		emp1.display();  
		emp2.display();  

	}

}

I hope you got the concept of Aggregation in Java. This blog post ends the Inheritance topics of the OOPs concept. We are going to discuss another chapter of the OOPs concept in subsequent blog posts. I hope you enjoyed reading this article. If you have any doubts, feel free to share your questions via below contact form and do not forget to connect with us through social media. Click on the below link to join our Facebook group to get the latest updates on Selenium, Java, and other topics on Test automation.

Join Inviul fb group

Leave a Reply