See the Tutorial List

Aggregation (HAS-A relationship) in Java

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

Let's take an example:

class Student
{
  String name;
  Address ad;
}

Here in the above code, you can say that Student has-a Address.

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.

Aggregation allows you to design classes that follow good Object Oriented practices. It also provide code reusability.


Example of Aggregation in Java

Let's take an example of aggregation in Java.

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;
  }
  
  public String getAuthorName()
  {
    return authorName;
  }
  public int getAge()
  {
    return age;
  }
  public String getPlace()
  {
    return place;
  }
}

class Book
{
  String name;
  int price;
  // author details
  Author auth;
  Book(String n,int p,Author at)
  {
    this.name = n;
    this.price = p;
    this.auth = at;
  }
  public void showDetail()
  {
    System.out.println("Book is" + name);
    System.out.println("price " + price);
    // using Author class funtion to get the name value
    System.out.println("Author is " + auth.getAuthorName());
  }
}

class Test
{
  public static void main(String args[])
  {
    Author auth = new Author("John", 22, "India");
    Book b = new Book("Java", 550, auth);
    b.showDetail();
 }
}

Book is Java. price is 550. Author is John.


Q. What is 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;
  }
}

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.