There is always been confusion about composition and aggregation. While in practical field went to understand 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.
VN:F [1.9.13_1145]
Rating: 7.4/10 (23 votes cast)
VN:F [1.9.13_1145]
Composition and Aggregation using Java, 7.4 out of 10 based on 23 ratings
About the author
Faisal Basra
Faisal Basra is an independent consultant, software developer, writer, blogger, speaker, architect and technology leader in Lahore, Pakistan. He has been a professional software developer since 2008, has been writing code since 2006.
Having hands on experience of popular Java EE frameworks & technologies like JSF, Spring, Hibernate, Enverse, JPA, Richfaces, Primefaces, JSP/Servlet.
I have taken many initiatives while working with teams. Some of includes Automated Build & Release Management system via Hudson, Maven, Archiva & SVN.
Blogging is my hobby and I also initiated blog at corporate level from setting up complete blog for company, content generation strategy and visibility over the Internet by Internet Marketing.
Framworks & Technologies:
JSF, Richfaces, Primefaces, Openfaces, Struts, Hibernate, Spring, ORMLite
Tools & Servers:
jUnit, Log4j, Maven, Eclipse, MyEclipse, NetBeans, Tomcat, Jboss, WebLogic
Mobile Development:
Google Android
Marketing:
Internet Marketing, Mobile App Marketing
Permanent link to this article: http://www.javaplex.com/blog/composition-and-aggregation-using-java/
Leave a Reply
19 comments
Balakrishnan says:
October 1, 2008 at 3:45 am (UTC 5)
Excellent explanation with very good example.
makiko_fly says:
September 21, 2009 at 11:43 am (UTC 5)
I am sorry, but where is the java code example?
sham says:
October 3, 2009 at 12:34 am (UTC 5)
yeah…m agree with makiko_fly that where is the java code…….
sham says:
October 3, 2009 at 12:52 am (UTC 5)
// 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 3, 2009 at 1:08 am (UTC 5)
// 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 (http://www NULL.sun NULL.com) says:
October 4, 2009 at 10:31 am (UTC 5)
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 10, 2010 at 12:24 pm (UTC 5)
thanks for your help.
Tabish Habib says:
June 2, 2010 at 1:36 am (UTC 5)
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.
Nadbme says:
July 25, 2011 at 7:15 am (UTC 5)
hi fiza ur code is wrong i use this code in programing so i give problem on run time kindly dnt write wrong code on net
thanks
nadeem
Nadbme says:
July 25, 2011 at 7:17 am (UTC 5)
fiza zaheer write wrong code probition on this that they avoid
thanks
nadeem
Shakeel Ahmad says:
September 26, 2011 at 7:30 am (UTC 5)
good explanation
Deepu says:
November 10, 2011 at 12:17 pm (UTC 5)
nice one
Hari Prasad says:
November 11, 2011 at 6:59 am (UTC 5)
nee podaaaa
Hari Prasad says:
November 11, 2011 at 6:59 am (UTC 5)
poda
Hari Prasad says:
November 11, 2011 at 6:59 am (UTC 5)
njan mandana
Hari Prasad says:
November 11, 2011 at 7:03 am (UTC 5)
dassad
Hari Prasad says:
November 11, 2011 at 7:05 am (UTC 5)
patticheeee
Hari Prasad says:
November 11, 2011 at 7:05 am (UTC 5)
ninte kunjamma
The Grafikkarte (http://grafikkarte85 NULL.tumblr NULL.com) says:
January 5, 2012 at 1:38 am (UTC 5)
Thanks a lot for providing individuals with an extraordinarily brilliant chance to read articles and blog posts from this web site. It is always very nice and also jam-packed with amusement for me and my office co-workers to search the blog particularly 3 times a week to read the fresh items you will have. And definitely, I am also actually fascinated considering the splendid tips and hints served by you. Selected 3 facts in this post are unequivocally the very best we have had.