AGGREGATION
Aggregation is one of the concept of oops
Aggregation is used to where one java class has
a reference to another java class
Aggregation is called as HAS-A relationship its
opposite of inheritance because inheritance is
called as IS-A relationship
Example for Aggregation sample program:
package javaprogram;
//Aggregation
class student
{
//create an instance variable of student name and
age
String username;
int age;
//using a constructor for create an local variable
link to instance variable using a this keyword
student(String username,int age)
{
this.username=username;
this.age=age;
}
}
// this both class are doesn't have any relation
class details
{
student studentdata; //create an reference for
student class
//constructor for details
details(student studentdata)
{
this.studentdata=studentdata;
}
//display method to execute a data of username
and age
void display()
{
System.out.println("student "+"
"+studentdata.username+" "+studentdata.age);
}
}
public class TopicScanner{
public static void main(String[] args) {
student studentclass=new student("Aravind",16);
//initial value for string and int student class
object
details detailsclass=new details(studentclass);
//details class object link to student class object
detailsclass.display(); // calling a
display method from details class
}
}
Using a aggregation like this we can create more data’s like
package javaprogram;
//Aggregation
class student
{
//create an instance variable of student name and
age
String username;
int age;
//using a constructor for create an local variable
link to instance variable using a this keyword
student(String username,int age)
{
this.username=username;
this.age=age;
}
}
// this both class are doesn't have any relation
class details
{
student studentdata; //create an reference for
student class
//constructor for details
details(student studentdata)
{
this.studentdata=studentdata;
}
//display method to execute a data of username
and age
void display()
{
System.out.println("student "+"
"+studentdata.username+" "+studentdata.age);
}
}
public class TopicScanner{
public static void main(String[] args) {
//using a aggregation we can create data's
using this type
student studentclassone=new student("Aravind",16);
// initial value for string and int student class
object
details detailsclassone=new details(studentclassone);
//details class object link to student class object
detailsclassone.display();//calling a display
method from details class
student studentclasstwo=new student("Aravind",16);
// initial value for string and int student class
object
details detailsclasstwo=new details(studentclasstwo);
//details class object link to student class object
detailsclasstwo.display();//calling a display
method from details class
student studentclassthree=new student("Aravind",16);
// initial value for string and int student class
object
details detailsclassthree=new details(studentclassthree);
//details class object link to student class object
detailsclassthree.display();//calling a display
method from details class
}
}
Difference between Inheritance and aggregation
Inheritance is used to inherit one java class to another
class for access data without creating an object
Inheritance is called as IS-A relationship
To access data from one java class to another java class
using a extends keyword
Aggregation is used to access class datas from one java
class to anoother java class create an reference name
Aggregation is called as HAS-A relationship
To access data from one java class to another java class
creating an reference name in java class