Home All Tutorials Core Java OOPs Collections Java I/O JSON DBMS
OOPs concepts – What is Aggregation in java? Search this website
BY CHAITANYA SINGH | FILED UNDER: OOPS CONCEPT
Aggregation is a special form of association. It is a relationship between two classes like
association, however its a directional association, which means it is strictly a one way association. It
represents a HAS-A relationship.
Aggregation Example in Java
For example consider two classes Student class and Address class. Every student has an address so
the relationship between student and address is a Has-A relationship. But if you consider its vice
versa then it would not make any sense as an Address doesn’t need to have a Student necessarily.
Lets write this example in a java program.
Student Has-A Address
class Address
{
int streetNum;
String city;
String state;
String country;
Address(int street, String c, String st, String coun)
{
this.streetNum=street;
this.city =c;
this.state = st;
this.country = coun;
}
}
class StudentClass
{
int rollNum;
String studentName;
//Creating HAS-A relationship with Address class
Address studentAddr;
StudentClass(int roll, String name, Address addr){
this.rollNum=roll;
this.studentName=name;
this.studentAddr = addr;
}
public static void main(String args[]){
Address ad = new Address(55, "Agra", "UP", "India");
StudentClass obj = new StudentClass(123, "Chaitanya", ad);
System.out.println(obj.rollNum);
System.out.println(obj.studentName);
System.out.println(obj.studentAddr.streetNum);
System.out.println(obj.studentAddr.city);
System.out.println(obj.studentAddr.state);
System.out.println(obj.studentAddr.country);
}
}
Output:
123
Chaitanya
55
Agra
UP
India
The above example shows the Aggregation between Student and Address classes. You can see that
in Student class I have declared a property of type Address to obtain student address. Its
a typical example of Aggregation in Java.
Why we need Aggregation?
To maintain code re-usability. To understand this lets take the same example again. Suppose there
are two other classes College and Staff along with above two classes Student and Address. In order
to maintain Student’s address, College Address and Staff’s address we don’t need to use the same
code again and again. We just have to use the reference of Address class while de ning each of
these classes like:
Student Has-A Address (Has-a relationship between student and address)
College Has-A Address (Has-a relationship between college and address)
Staff Has-A Address (Has-a relationship between staff and address)
Hence we can improve code re-usability by using Aggregation relationship.
So if I have to write this in a program, I would do it like this:
class Address
{
int streetNum;
String city;
String state;
String country;
Address(int street, String c, String st, String coun)
{
this.streetNum=street;
this.city =c;
this.state = st;
this.country = coun;
}
}
class StudentClass
{
int rollNum;
String studentName;
//Creating HAS-A relationship with Address class
Address studentAddr;
StudentClass(int roll, String name, Address addr){
this.rollNum=roll;
this.studentName=name;
this.studentAddr = addr;
}
...
}
class College
{
String collegeName;
//Creating HAS-A relationship with Address class
Address collegeAddr;
College(String name, Address addr){
this.collegeName = name;
this.collegeAddr = addr;
}
...
}
class Staff
{
String employeeName;
//Creating HAS-A relationship with Address class
Address employeeAddr;
Staff(String name, Address addr){
this.employeeName = name;
this.employeeAddr = addr;
}
...
}
As you can see that we didn’t write the Address code in any of the three classes, we simply created
the HAS-A relationship with the Address class to use the Address code. The dot dot(…) part in the
above code can be replaced with the public static void main method, the code in it would be similar
to what we have seen in the rst example.
❮ Previous Next ❯
Comments
Arunkumar says
FEBRUARY 7, 2014 AT 9:21 AM
It says NULL pointer Exception in this line
System.out.println(obj.studentAddr.streetNum);
Reply
Chaitanya Singh says
FEBRUARY 7, 2014 AT 12:54 PM
Hi ArunKumar,
Did you copy the code correctly? Please verify it once. The code given in the tutorial
is already tested and it is not throwing any error.
Reply
Arunkumar says
FEBRUARY 7, 2014 AT 9:36 AM
This is my program and it shows
package Second_Java;
class Aggregation_Example {
String name;
int age;
public Aggregation_Example(String name,int age)
{
this.name=name;
this.age=age;
}
}
class Student
{
Aggregation_Example agrn;
String address;
Student(String address)
{
this.address=address;
}
public static void main(String x[])throws Exception
{
Aggregation_Example btn=new Aggregation_Example("arun",23);
Student stud=new Student("Chennai");
System.out.println(stud.address);
System.out.println(stud.agrn.age);
System.out.println(stud.agrn.name);
Reply
Nisha says
JUNE 5, 2014 AT 2:11 PM
Hi Arun,
It seems like you didnt understood the purpose of aggregation. You havent used it
in your program. I have made some changes in your program. Hope it helps..
public class Aggregation_Example {
String name;
int age;
String addre;
public Aggregation(String name,int age,String addre)
{
this.name=name;
this.age=age;
this.addre=addre;//this you should use cos this makes a relationship with your
another class
}
public static void main(String x[])throws Exception// specify main method in your
main class
{
Aggregation_Example btn=new Aggregation_Example (“arun”,23,”Chennai”);
// you should pass btn object which contains address which you can access in
Student class
Student stud=new Student(“10th”,’A’,btn);
System.out.println(stud.agrn.age);
System.out.println(stud.agrn.name);
System.out.println(stud.agrn.addre);
}
}
class Student
{
Aggregation_Example agrn;
String nameClass;
char c;
Student(String classWhich,char section,Aggregation_Example address)
{
this.nameClass=classWhich;
this.c=section;
this.agrn=address; // you havent used in your example which raised a nullpointer
exception
}
}
Reply
Ilir says
MAY 27, 2014 AT 5:34 PM
Thanks for the explanation. I am taking the OCA Java SE7 Programmer I exam, and
needed to understand aggregation. Now I have an understanding of aggregation. Good
example. I didn’t try it out, but I came for understanding the mean of aggregation, which
I understand now.
Reply
AmanG says
APRIL 2, 2016 AT 4:30 AM
hello, please tell me some important less known points about aggregation which i must
know while explaining it to anyone.
Reply
Gautam says
AUGUST 17, 2016 AT 7:11 AM
Here is a simple example program on aggregation
class HardDisk
{
public void writedata()
{
System.out.println(“data is being written”);
}
}
class Dell
{
public static void main(String[]args)
{
HardDisk segate= new HardDisk();
segate.writedata();
}
}
*** class A is in HAS-A relationship with Class B if Class A holds the reference of class B
HERE segate is a reference of HardDisk in class Dell
so, Dell HAS-A HardDisk
Reply
Anurag says
FEBRUARY 23, 2017 AT 11:15 AM
Does It means that aggregation is closely related to inheritance and it has much to do
with access speci er?
Reply
abhi says
MARCH 23, 2017 AT 2:58 AM
Hi,
Lets say an Interface and a Class.
interface I {
}
class C {
}
Is it possible to have a Has-A relation between these two ? Please reply.
Reply
Leave a Reply
Your email address will not be published. Required elds are marked *
Comment
Name *
Email *
POST COMMENT
Java Tutorial
Java Index
Java Introduction
JVM - Java Virtual
Machine
First Java Program
Variables
Data Types
Operators
Java Control
Statements
Java If-else
Java Switch-Case
Java For loop
Java while loop
Java do-while loop
Continue statement
break statement
OOPs Concepts
OOPs Concepts
Constructor
Static keyword
Inheritance
Types of inheritance
Aggregation
Association
Super Keyword
Method overloading
Method overriding
Overloading vs
Overriding
Polymorphism
Types of
polymorphism
Static and dynamic
binding
Abstract class and
methods
Interface
Abstract class vs
interface
Encapsulation
Packages
Access modi ers
Garbage Collection
Inner classes
Static import
Static constructor
Java Interview Q
MORE ...
Java 8 Features
Java 9 Features
Java Conversion
Java String
Exception handling
Java Multithreading
Java I/O
Java Serialization
Java Regex
Java AWT
Java Swing
Java Enum
Java Annotations
Copyright © 2012 – 2020 BeginnersBook . Privacy Policy . Sitemap