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

Aggregation in Java With Examples

Aggregation in Java represents a HAS-A relationship where one class contains an instance of another class. For example, an Employee class may contain an Address class to store an employee's address details. Aggregation allows for code reuse through method delegation. It is used when there is no "is-a" relationship between classes, but one class needs to use the properties or behaviors of another. A common example is a Circle class containing an Operation class to reuse the square method for calculating the circle's area.

Uploaded by

Chaitali Dhawan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Aggregation in Java With Examples

Aggregation in Java represents a HAS-A relationship where one class contains an instance of another class. For example, an Employee class may contain an Address class to store an employee's address details. Aggregation allows for code reuse through method delegation. It is used when there is no "is-a" relationship between classes, but one class needs to use the properties or behaviors of another. A common example is a Circle class containing an Operation class to reuse the square method for calculating the circle's area.

Uploaded by

Chaitali Dhawan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Aggregation in Java

If a class have an entity reference, it is known as Aggregation. Aggregation


represents HAS-A relationship.

Consider a situation, Employee object contains many informations such as id, name,
emailId etc. It contains one more object named address, which contains its own
informations such as city, state, country, zipcode etc. as given below.

1. class Employee{  
2. int id;  
3. String name;  
4. Address address;//Address is a class  
5. ...  
6. }  

Simple Example of Aggregation


1. class Operation{  
2.  int square(int n){  
3.   return n*n;  
4.  }  
5. }  
6.   
7. class Circle{  
8.  Operation op;//aggregation  
9.  double pi=3.14;  
10.     
11.  double area(int radius){  
12.    op=new Operation();  
13.    int rsquare=op.square(radius);//code reusability (i.e. delegates the method c
all).  
14.    return pi*rsquare;  
15.  }  
16.   
17.      
18.     
19.  public static void main(String args[]){  
20.    Circle c=new Circle();  
21.    double result=c.area(5);  
22.    System.out.println(result);  
23.  }  
24. }  

When use Aggregation?


o Code reuse is also best achieved by aggregation when there is no is-a relationship.
o 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.

Understanding meaningful example of Aggregation


In this example, Employee has an object of Address, address object contains its own
informations such as city, state, country etc. In such case relationship is Employee
HAS-A address.

Address.java
1. public class Address {  
2. String city,state,country;  
3.   
4. public Address(String city, String state, String country) {  
5.     this.city = city;  
6.     this.state = state;  
7.     this.country = country;  
8. }  
9.   
10. }  
Emp.java
1. public class Emp {  
2. int id;  
3. String name;  
4. Address address;  
5.   
6. public Emp(int id, String name,Address address) {  
7.     this.id = id;  
8.     this.name = name;  
9.     this.address=address;  
10. }  
11.   
12. void display(){  
13. System.out.println(id+" "+name);  
14. System.out.println(address.city+" "+address.state+" "+address.country);  
15. }  
16.   
17. public static void main(String[] args) {  
18. Address address1=new Address("gzb","UP","india");  
19. Address address2=new Address("gno","UP","india");  
20.   
21. Emp e=new Emp(111,"varun",address1);  
22. Emp e2=new Emp(112,"arun",address2);  
23.       
24. e.display();  
25. e2.display();  
26.       
27. }  
28. }  

You might also like