0% found this document useful (0 votes)
2 views

15 Java Aggregation PDF

Aggregation is a concept in object-oriented programming (OOP) that represents a HAS-A relationship between classes, contrasting with inheritance's IS-A relationship. The document provides a sample Java program demonstrating aggregation through a 'student' class and a 'details' class, showcasing how one class can reference another. It also outlines the differences between inheritance and aggregation, emphasizing their respective purposes and relationships in Java programming.

Uploaded by

Vignesh gs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

15 Java Aggregation PDF

Aggregation is a concept in object-oriented programming (OOP) that represents a HAS-A relationship between classes, contrasting with inheritance's IS-A relationship. The document provides a sample Java program demonstrating aggregation through a 'student' class and a 'details' class, showcasing how one class can reference another. It also outlines the differences between inheritance and aggregation, emphasizing their respective purposes and relationships in Java programming.

Uploaded by

Vignesh gs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like