Composition and Aggregation using Java
Sunday, June 29, 2008 1:22There is always been confusion about composition and aggregation. While in servicing NetSol Technologies, I came to learn practically what is composition and aggregation. First thing is both are type of association. Below is the exact definition of each term and then practical Java code, where you can understand exactly how can we implement composition and aggregation in programming.
Composition: Life time of objects got to be the same. For example there is a composition between car-frame and a car. If the car gets destroyed the car-frame would also get destroyed.
Aggregation: Life times of the objects are not the same, one object can live even after the other object has been destroyed.

Balakrishnan says:
October 1st, 2008 at 3:45 am
Excellent explanation with very good example.
makiko_fly says:
September 21st, 2009 at 11:43 am
I am sorry, but where is the java code example?
sham says:
October 3rd, 2009 at 12:34 am
yeah…m agree with makiko_fly that where is the java code…….
sham says:
October 3rd, 2009 at 12:52 am
// ShambhuDubey: Aggregation Example
import java.util.ArrayList;
public class Aggregation {
public static void main(String[] args)
{
MusicPlayer player = new MusicPlayer();
Car car = new Car();
car.addPlayer(player);
}
}
/**
* Client, Aggregate, Whole
*/
class Car {
private ArrayList players;
public Car() {
// players list is empty, we can attach players via addPlayer(), so
// that car CONTAINS players, however our car object is not dependant
// on player for initialization – we can initialize car objects even
// if we have no intention to put player into them
players = new ArrayList();
}
public void addPlayer(MusicPlayer p)
{
players.add(p);
}
}
/**
* Dump MusicPlayer class
*/
class MusicPlayer {}
sham says:
October 3rd, 2009 at 1:08 am
// ShambhuDubey: Composition Example
import java.util.ArrayList;
public class Composition {
public static void main(String[] args) {
// Frame objects life-cycle is totally under container class controll
Car car = new Car();
}
}
class Car {
private ArrayList frames;
public Car() {
frames = new ArrayList();
frames.add(new CarFrame());
}
}
class CarFrame {}
Fiza Zaheer says:
October 4th, 2009 at 10:31 am
Hi, Let me explain it via the code example.
Product, Order & OrderLine are the three entities.
Product relationship with OrderLine is example of Aggregation.
Order relationship with OrderLine is example of Composition.
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Order order = new Order();
Product product = new Product();
product.setName(”BMAW”);
product.setPrice(9786);
order.addOrderLine(product);
order = null; // OrderLines are destroyed along with order
//Because orderLine and Order have strong relationships i.e. composition.
//But Product will exist, even orderLine is deleted because it have weaker relationship
//i.e. Composition.
}
}
class OrderLine {
Product product;
int quantity;
// Add many attribute per you order line.
public Product getProduct() {
return this.product;
}
public void setProduct(Product product) {
this.product = product;
}
}
class Product {
private String name;
private int price;
// Add many attributes as you need.
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
class Order {
private List lines = new ArrayList();
String customerName;
// Add many order attributes as you need.
public boolean addOrderLine(Product product) {
OrderLine line = new OrderLine();
line.setProduct(product);
lines.add(line);
return true;
}
}
basanta nandi says:
March 10th, 2010 at 12:24 pm
thanks for your help.
Tabish Habib says:
June 2nd, 2010 at 1:36 am
Composition is strong relationship: A Composes B, means A contains ans owns B i.e. when A is destroyed B will also be destroyed. Aggreagartion: Aggregration between A and B means, A contains but not owns B i.e. when A is destroyed B remains intact.