Signup/Sign In

Aggregation (HAS-A relationship) in Java

Aggregation is a term which is used to refer one way relationship between two objects. For example, Student class can have reference of Address class but vice versa does not make sense.

In Java, aggregation represents HAS-A relationship, which means when a class contains reference of another class known to have aggregation.

The HAS-A relationship is based on usage, rather than inheritance. In other words, class A has-a relationship with class B, if class A has a reference to an instance of class B.

Lets understand it by an example and consider two classes Student and Address. Each student has own address that makes has-a relationship but 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;
}

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

Aggregation 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 relation with some other entity than it can reuse code just by referring that.


Aggregation Example

Now lets understand it by using an example, here we created two classes Author and Book and Book class has a relation with Author class by having its reference. Now we are able to get all the properties of both class.

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("John", 42, "USA");
    Book b = new Book("Java for Begginer", 800, 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);
  }
  
}

Book Name: Java for Begginer Book Price: 800 ------------Author Details---------- Auther Name: John Auther Age: 42 Auther place: USA

Lets take one more 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. Lets understand it by Java code.

  
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("John", 42, "USA");
    Publisher publisher = new Publisher("Sun Publication","JDSR-III4", "LA");
    Book b = new Book("Java for Begginer", 800, 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);
  }
  
}
  

Book Name: Java for Begginer Book Price: 800 ------------Author Details---------- Auther Name: John Auther Age: 42 Auther place: USA ------------Publisher Details------- Publisher Name: Sun Publication Publisher ID: JDSR-III4 Publisher City: LA

Composition in Java

Composition is a more restricted form of Aggregation. Composition can be described as when one class which includes another class, is dependent on it in such a way that it cannot functionally exist without the class which is included. For example a class Car cannot exist without Engine, as it won't be functional anymore.

Hence the word Composition which means the items something is made of and if we change the composition of things they change, similarly in Java classes, one class including another class is called a composition if the class included provides core functional meaning to the outer class.

class Car
{
  private Engine engine;
  Car(Engine en)
  {
    engine = en;
  }
}

Here by examining code, we can understand that if Car class does not have relationship with Engine class then Car does not have existence.

Composition is a design technique, not a feature of Java but we can achieve it using Java code.


Q. When to use Inheritance and Aggregation?

When you want to use some property or behaviour of any class without the requirement of modifying it or adding more functionality to it, in such case Aggregation is a better option because in case of Aggregation we are just using any external class inside our class as a variable.

Whereas when you want to use and modify some property or behaviour of any external class or may be want to add more function on top of it, its best to use Inheritance.

To understand more on inheritance, you can visit our detailed tutorial here. Click Here to see Inheritance in Java