Aggregation (HAS-A relationship) in Java

When we talk about aggregation than aggregation is a form of association. Aggregation is used to refer one-way relationship between two objects.

In java aggregation means HAS-A relationship, it means when class contains reference of another class known as aggregation.

Aggregation is a way of composing different abstractions together in defining a class. For example, a car class can be limited to contain other classes such as engine class, seat class, wheels class etc. The car class can define an engine class as one of its attributes. A concrete instance of a car class will have its attribute as engine which will hold a concrete instance of engine class. For example, two instances of a car class fiat and ford will have their engine attributes which will keep a ford engine and fiat engine objects respectively. Objects could share other objects. When ford and fiat are using the same engine design, they can share one engine design object. In such a case, engine design object is said to be nominally part of car design object.

Other examples of aggregation are A window class containing menu class, check-box class etc.

Example of aggregation

A car object is an aggregation of engine, seat, wheels and other objects.

Aggregation (HAS-A relationship) in Java
Another Example

Consider two classes Student and Address. Each student has own address that makes has-a relationship, but the address has student not makes any sense. We can understand it more clearly using Java code.

		Class Address{
		int street_no;
		String city;
		String state;
		int pin;
		Address(int street_no, String city, String state, int pin ){
		this.street_no = street_no;
		this.city = city;
		this.state = state;
		this.pin = pin;
		}
		}


		class Student
		{
			String name;
		    Address ad;
		}
	

In the above code, we can see Student class has-a relationship with Address class. We have drawn an image too to demonstrate relationship between these two classes.

Aggregation (HAS-A relationship) in Java

The Student class has an instance variable of type Address. As we have a variable of type Address in the Student class, it can use Address reference, which is ad, in this case, to invoke methods of the Address class.

Advantage of Aggregation

The main advantage of using aggregation is to maintain code re-usability. If an entity has a relationship with some other entity than it can reuse code just by referring that.

Aggregation Example
		class Author
		{
		  String authorName;
		  int age;
		  String place;
		  
		  // Author class constructor
		  Author(String name, int age, String place)
		  {
		    this.authorName = name;
		    this.age = age;  
		    this.place = place;
		  }
		}


		class  Book
		{
		  String name;
		  int price;
		  // author details
		  Author auther;
		  Book(String n, int p, Author auther)
		  {
		    this.name = n;
		    this.price = p;
		    this.auther = auther;
		  }
		  public static void main(String[] args) {
		    Author auther = new Author("Manish", 42, "INDIA");
		    Book b = new Book("Ducat Tutorial", 500, auther);
		    System.out.println("Book Name: "+b.name);
		    System.out.println("Book Price: "+b.price);
		    System.out.println("------------Auther Details----------");
		    System.out.println("Auther Name: "+b.auther.authorName);
		    System.out.println("Auther Age: "+b.auther.age);
		    System.out.println("Auther place: "+b.auther.place);
		  }


		}
	
Output

Book Name: Ducat Tutorial

Book Price: 500

------------Author Details----------

Auther Name: Manish

Auther Age: 42

Auther place: INDIA

Example2

Now take another example to understand aggregation. Suppose we have one more class Publisher then the Book class can reuse Publisher class details by just using its reference as Author class.

		class Publisher{


		  String name;
		  String publisherID;
		  String city;


		  Publisher(String name, String publisherID, String city) {
		    this.name = name;

		    this.publisherID = publisherID;
		    this.city = city;
		  }
		}


		class Author
		{
		  String authorName;
		  int age;
		  String place;
		  // Author class constructor
		  Author(String name, int age, String place)
		  {
		    this.authorName = name;
		    this.age = age;
		    this.place = place;
		  }
		}
		class Book
		{
		  String name;
		  int price;
		  // author details
		  Author auther;
		  Publisher publisher;
		  Book(String n, int p, Author auther, Publisher publisher )
		  {
		    this.name = n;
		    this.price = p;
		    this.auther = auther;
		    this.publisher = publisher;
		  }
		  public static void main(String[] args) {
		    Author auther = new Author("Manish", 42, "INDIA");
		    Publisher publisher = new Publisher("Sun Publication","WSHO-III9","NOIDA");
		    Book b = new Book("Ducat Tutorial", 500, auther, publisher);
		    System.out.println("Book Name: "+b.name);
		    System.out.println("Book Price: "+b.price);
		    System.out.println("------------Author Details----------");
		    System.out.println("Auther Name: "+b.auther.authorName);
		    System.out.println("Auther Age: "+b.auther.age);
		    System.out.println("Auther place: "+b.auther.place);
		    System.out.println("------------Publisher Details-------");
		    System.out.println("Publisher Name: "+b.publisher.name);
		    System.out.println("Publisher ID: "+b.publisher.publisherID);
		    System.out.println("Publisher City: "+b.publisher.city);
		  }


		}
	
Output

Book Name: Ducat Tutorial

Book Price: 500

------------Author Details----------

Auther Name: Manish

Auther Age: 42

Auther place: INDIA

------------Publisher Details-------

Publisher Name: Sun Publication

Publisher ID: WSHO-III9

Publisher City: NOIDA

Copyright 1999- Ducat Creative, All rights reserved.