Aggregation in Java: Definition and Examples

Rumman Ansari   Software Engineer   2024-07-04 03:41:41   7908  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

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. So, If a class have an entity reference, it is known as Aggregation.

An object can contain another object. The relationship between the two is called composition.Composition is actually a special case of the aggregation relationship.

Aggregation models has-a relationships and represents an ownership relationship between two objects. The owner object is called an aggregating object and its class an aggregating class. The subject object is called an aggregated object and its class an aggregated class.

When use Aggregation?

  • Code reuse is also best achieved by aggregation when there is no is-a relationship.
  • Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice. When you need to use property and behaviour of a class without modifying it inside your class. In such case Aggregation is a better option. Whereas when you need to use and modify property and behaviour of a class inside your class, its best to use Inheritance.

Example:

Consider a situation, Student object contains many informations such as roll_no, name, etc. It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.
 class Student
{
 int roll_no;
 String name;
 Address adddress;
}
 

Here you can say that Student has-a Address.

Why use Aggregation?

For Code Reusability.

Understanding example of Aggregation

Save: Address.java
 public class Address {
String city,state,country;

public Address(String city, String state, String country) {
    this.city = city;
    this.state = state;
    this.country = country;
 }
}
 
Save: Student.java
 public class Student {
int rollNo;
String name;
Address address;

public Student(int rollNo, String name,Address address) {
    this.rollNo = rollNo;
    this.name = name;
    this.address=address;
}

void display(){
 System.out.println(rollNo+" "+name);
 System.out.println(address.city+" "+address.state+" "+address.country);
}

public static void main(String[] args) {
 Address address1=new Address("Burdwan","WB","india");
 Address address2=new Address("Hoogly","WB","india");

 Student e=new Student(1,"Rumman",address1);
 Student e2=new Student(2,"Jaman",address2);

 e.display();
 e2.display();

 }
}
 
 1 Rumman
Burdwan WB india
2 Jaman
Hoogly WB india
Press any key to continue . . .
 

Important Example of Aggregation


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


class Author
{
	 String authorName;
 	 int age;
	 String place;

 		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 auth;
 Book(String na,int pr,Author at)
 {
  this.name=na;
  this.price=pr;
  this.auth=at;
 }
 public void showDetail()
 {
  System.out.println("Book is "+name);
  System.out.println("price "+price);
  System.out.println("Author is "+auth.getAuthorName());
 }
}
 
Book is Java
price 550
Author is Hero Alom
Press any key to continue . . .
 

What is an aggregation relationship between two objects?

Aggregation models has-a relationships and represents an ownership relationship between two objects.

What is a composition relationship between two objects?

An object can contain another object. The relationship between the two is called composition.

In this example, we have created the reference of Operation class in the Circle class.

class Operation{
 int square(int n){
  return n*n;
 }
}

class Circle{
 Operation op;//aggregation
 double pi=3.14;

 double area(int radius){
   op=new Operation();
   int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
   return pi*rsquare;
 }



 public static void main(String args[]){
   Circle c=new Circle();
   double result=c.area(8);
   System.out.println(result);
 }
}
 
 
 200.96
Press any key to continue . . .