Association – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 20 Jun 2020 17:04:23 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Association – Programmerbay https://programmerbay.com 32 32 Difference between Association and Aggregation in Java https://programmerbay.com/difference-between-association-and-aggregation-in-java/ https://programmerbay.com/difference-between-association-and-aggregation-in-java/#respond Wed, 14 Aug 2019 15:24:32 +0000 https://www.programmerbay.com/?p=4646 Association

It can be defined as the relationship where two different classes communicate with each other for some purpose. Any kind of relationship if exists between two classes then they are said to be associated. The lifetime of an object of a class is not dependent on any other class’s object. It can be a bi-directional relationship.

association

Let’s look at an example of implementing an association in Java.

Program:

class School
{
private String name; 
School(String name) 
{ this.name = name; } 
public String getSchool() 
{ return this.name; } 
}   

class Student 
{ 
private String name; 
Student(String name)  
{ this.name = name; } 
public String getStudent() 
{ return this.name; }  
} 
 
public Class Example
{ 
public static void main (String[] args)  
{ 
School sc = new School("A"); 
Student st = new Student("xxx"); 
          
System.out.println(emp.getStudent() + " is student of " + bank.getSchool()); 
} 
}

Can an aggregation be an association?

Yes. Every aggregation is an association.

Aggregation

Basically, aggregation is a type of association but in aggregation, the relationship between two classes is called Has-A relationship. It is a unidirectional relationship. In this, both the classes can exist independently i.e. if one of the class doesn’t exist that will not affect the other class. In aggregation one class is the owner and one is the part class. The part only has a reference variable in the owner class.

aggergationn

Let’s look at an example of implementing an Aggregation in Java.

Program:

public class Address {  
String add;  
public Address(String add) {  
this.add = add; 
}  
}

public class Emp {  
int id;  
Address address;  
public Emp(int id, Address address) {  
this.id = id;   
this.address=address;  
}  
void display(){  
System.out.println(id);  
System.out.println(address.add);  
}  

public static void main(String[] args) {  
Address a1=new Address(“Delhi”);  
Address a2=new Address(“UP”);  
Emp e=new Emp(1, a1);  
Emp e2=new Emp(2, a2);  
e.display();  
e2.display();  
}  
}

 

In the above example, we have two classes Address and Emp. Emp has a reference variable of Address class address. The address in “Emp” class is dependent on the add from Address class.

Difference between Association and Aggregation in Java

  • In association, both the classes are independent of each other. In aggregation, one class is a part of another class
  • Association has a bi-directional relationship, whereas aggregation has a unidirectional relationship between the classes.
  • All aggregation relationships are also association but not all associations are called aggregation relation.
  • Example of association is School and Student where both can exist without each other. Example of aggregation is Employee and Address where Address is a part of Employee class.

Key Differences:

AssociationAggregation
Classes are independent from one anotherOne class is a part of another class, It can't exist independently
It  has a bi-directional relationshipIt has a unidirectional relationship between the classes
A line is used to represent AssociationA line with diamond shape is used to represent Aggregation
School and Student where both can exist without each otherEmployee and Address where Address is a part of Employee class

 

 

]]>
https://programmerbay.com/difference-between-association-and-aggregation-in-java/feed/ 0