Aggregation in Java


JavaViews 3877

Aggregation in Java allows us to provide a reference for one class within another class. In other words, it supports a one-way relationship and implements a HAS-A relation. This is in contrast to the java inheritance concept that supports IS-A relation.

When two classes have a HAS-A relation, we say it implements aggregation. One best example we can think of is “Employee has a Car“. We say this is unidirectional since a car cannot have an employee and only an employee can have a car.

Before we understand this with an example, let see why we need to use aggregation.

Usage of Aggregation in java

  • Provides reusability of code
  • We can directly access the methods and variables of the reference class
  • Implements HAS-A relation

Example of Aggregation

Let us see how we can implement “Employee has a Car” in java using the aggregation concept. We create 2 classes Car and Employee. In the Employee class, we create a reference to the Car class by creating an object. Hence using this car object we can access all the properties of the Car class. So the Employee class has a unidirectional relationship with the Car class that implements aggregation.

This is a realtime example of aggregation where the company can track which employees are coming in their own vehicle to provide vehicle pass.

class Car {
  String carname;
  String color;
  String regno;
  
  Car(String carname, String color, String regno) {
    this.carname = carname;
    this.color = color;
    this.regno = regno;
  }
  
}
public class Employee {
  
  String empname;
  int empid;
  Car car;
  
  Employee(String empname, int empid, Car car) {
    this.empname = empname;
    this.empid = empid;
    this.car = car;
  }

  public static void main(String[] args) {
    Car car = new Car("Dezire","blue","KA 02 B 5544");
    Employee e = new Employee("Raj",456234,car);
    System.out.println("----------Employee Details:------------");
    System.out.println("Employee Name: " + e.empname);
    System.out.println("Employee ID: " + e.empid);
    System.out.println("----------Employee's car details-------");
    System.out.println("Car Name: " + e.car.carname);
    System.out.println("Car color: " + e.car.color);
    System.out.println("Car Reg No: " + e.car.regno);

  }

}
----------Employee Details:------------
Employee Name: Raj
Employee ID: 456234
----------Employee's car details-------
Car Name: Dezire
Car color: blue
Car Reg No: KA 02 B 5544

There may be situations where an Employee can register for both car and bike. So in this case, we can also have another relationship where the “Employee has a Bike”. Let’s see this aggregation implementation with java code.

Aggregation in Java

So now in the Employee class, we have 2 references, 1 to the Car class and another to the Bike class. Hence a single class can have a unidirectional relationship with multiple classes that implement the aggregation concept.

class Car {
  String carname;
  String color;
  String regno;
  
  Car(String carname, String color, String regno) {
    this.carname = carname;
    this.color = color;
    this.regno = regno;
  }
  
}

class Bike {
  String bikename;
  String regno;
  
  Bike(String bikename, String regno) {
    this.bikename = bikename;
    this.regno = regno;
  }
}
public class Employee {
  
  String empname;
  int empid;
  Car car;
  Bike bike;
  
  Employee(String empname, int empid, Car car, Bike bike) {
    this.empname = empname;
    this.empid = empid;
    this.car = car;
    this.bike = bike;
  }

  public static void main(String[] args) {
    Car car = new Car("Dezire","blue","KA 02 B 5544");
    Bike bike = new Bike("Pulsar","KA 04 C 2233");
    Employee e = new Employee("Raj",456234,car,bike);
    System.out.println("----------Employee Details:------------");
    System.out.println("Employee Name: " + e.empname);
    System.out.println("Employee ID: " + e.empid);
    System.out.println("----------Employee's car details-------");
    System.out.println("Car Name: " + e.car.carname);
    System.out.println("Car color: " + e.car.color);
    System.out.println("Car Reg No: " + e.car.regno);
    System.out.println("----------Employee's bike details-------");
    System.out.println("Bike Name: " + e.bike.bikename);
    System.out.println("Bike Reg No: " + e.bike.regno);

  }

}
----------Employee Details:------------
Employee Name: Raj
Employee ID: 456234
----------Employee's car details-------
Car Name: Dezire
Car color: blue
Car Reg No: KA 02 B 5544
----------Employee's bike details-------
Bike Name: Pulsar
Bike Reg No: KA 04 C 2233

Let’s see another example of aggregation where we can reuse the same class as a reference for different classes that enables one-way relation.

In the below example, we are demonstrating the reusability of the code using aggregation in java. In all these cases, we are reusing the class Bike where we give reference of this class in Employee, Student, and Teacher classes.

An Employee has a bike

A student has a bike

A teacher has a bike

//Bike.java
class Bike {
  String bikename;
  String regno;
  
  Bike(String bikename, String regno) {
    this.bikename = bikename;
    this.regno = regno;
  }
}

//Employee.java
public class Employee {
  
  String empname;
  int empid;
  Car car;
  Bike bike;
  
  Employee(String empname, int empid, Car car, Bike bike) {
    this.empname = empname;
    this.empid = empid;
    this.car = car;
    this.bike = bike;
  }

  public static void main(String[] args) {
    Car car = new Car("Dezire","blue","KA 02 B 5544");
    Bike bike = new Bike("Pulsar","KA 04 C 2233");
    Employee e = new Employee("Raj",456234,car,bike);
    System.out.println("----------Employee Details:------------");
    System.out.println("Employee Name: " + e.empname);
    System.out.println("Employee ID: " + e.empid);
    System.out.println("----------Employee's car details-------");
    System.out.println("Car Name: " + e.car.carname);
    System.out.println("Car color: " + e.car.color);
    System.out.println("Car Reg No: " + e.car.regno);
    System.out.println("----------Employee's bike details-------");
    System.out.println("Bike Name: " + e.bike.bikename);
    System.out.println("Bike Reg No: " + e.bike.regno);

  }

//Student.java	
  class Student {
    String studentname;
    int rollno;
    Bike bike;
    
    Student(String name, int no, Bike bike) {
      this.studentname = name;
      this .rollno = no;
      this.bike = bike;
          
    }
    
    public static void main(String[] args) {
      //include code same as Employee class
    }
  }

//Teacher.java	
  class Teacher {
    String teachername;
    int id;
    Bike bike;
    
    Teacher(String name, int id, Bike bike){
      this.teachername = name;
      this.id = id;
      this.bike = bike;
    }
    
    public static void main(String[] args) {
      //include code same as Employee class
    }
  }

}

Conclusion

In this tutorial, we have learned how to use aggregation in different ways to implement a HAS-A relationship.

Reference

Translate »