Aggregation:
Aggregation is one of the special case of Association. The directional association present in between the objects can also be determined. When a single object ‘has-a’ another object and you will get the aggregation. The directions between the object will be specified with the object containing other object. Aggregation can also be called as “Has-a” relationship.
Example : Employee Has a Address
Composition:
Composition is also one of the special case for the aggregation. The restricted aggregation can be called as composition so when an object has another object then contained object will not exist. So without the existence of the container object they can be called as the composition.
Difference between Aggregation and Composition
Composition is the restrictive so if there are any compositions between two objects, then composed object will not exist without other object. So the restriction will not be present in the Aggregation but a single object can also contain many other objects.
Example: Consider the bellow scenario.
Organization, People, Accounts. An Organization uses the People and owns the Accounts. But in the relationship point of view both (People,Accounts) are having the “Has a” relationship with Organization.
Here Organization is an aggregation of People and Organization is a composition of Accounts. I.e when a Organization ceases to do business its Accounts are closed (there is no existence of Accounts with out Organization) but People are continue to exist.
Example Source Code:
[sourcecode language=”java”]
/**
*
* @author chandrashekhar
*/
public class AggregationVsComposition {
public static void main(String[] args) {
People people = new People();
people.setId(1);
people.setName("Rahul");
Accounts accounts = new Accounts();
accounts.setType(1);
accounts.setName("Company-Accounts");
accounts.setBalance(100000);
Organization organization = new Organization();
organization.setName("OnlineTutorialsPoint");
organization.setCaption("AggregationVsComposition Tutorials");
organization.setAccounts(accounts);
organization.setPeople(people);
System.out.println("Organisation : "+organization);
}
}
class Organization {
private String name;
private String caption;
People people; // uses People
Accounts accounts; // owns Accounts
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
public Accounts getAccounts() {
return accounts;
}
public void setAccounts(Accounts accounts) {
this.accounts = accounts;
}
@Override
public String toString() {
return "Organization{" + "name=" + name + ", caption=" + caption + ", people=" + people + ", accounts=" + accounts + ‘}’;
}
}
class People {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" + "id=" + id + ", name=" + name + ‘}’;
}
}
class Accounts {
private int type;
private String name;
private double balance;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Accounts{" + "type=" + type + ", name=" + name + ", balance=" + balance + ‘}’;
}
}
OutPut:
Organization{name=OnlineTutorialsPoint, caption=AggregationVsComposition Tutorials, people=People{id=1, name=Rahul}, accounts=Accounts{type=1, name=Company-Accounts, balance=100000.0}}
<pre>[/sourcecode]