«

»

Jun
29

Composition and Aggregation using Java

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]
Rating: 0 (from 4 votes)
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/

19 comments

  1. Balakrishnan says:

    Excellent explanation with very good example.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  2. makiko_fly says:

    I am sorry, but where is the java code example?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  3. sham says:

    yeah…m agree with makiko_fly that where is the java code……. ;)

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  4. sham says:

    // 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 {}

    VA:F [1.9.13_1145]
    Rating: 3.0/5 (2 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 3 votes)
  5. sham says:

    // 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 {}

    VA:F [1.9.13_1145]
    Rating: 4.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
  6. Fiza Zaheer (http://www NULL.sun NULL.com) says:

    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;
    }
    }

    VA:F [1.9.13_1145]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
  7. basanta nandi says:

    thanks for your help.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  8. Tabish Habib says:

    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.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  9. Nadbme says:

    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 

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  10. Nadbme says:

    fiza zaheer write wrong code probition on this that they avoid
    thanks
    nadeem

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  11. Shakeel Ahmad says:

    good explanation

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  12. Deepu says:

    nice one

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  13. Hari Prasad says:

    nee podaaaa

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  14. Hari Prasad says:

    poda

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  15. Hari Prasad says:

    njan mandana

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  16. Hari Prasad says:

    dassad

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  17. Hari Prasad says:

    patticheeee

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  18. Hari Prasad says:

    ninte kunjamma

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  19. The Grafikkarte (http://grafikkarte85 NULL.tumblr NULL.com) says:

    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.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>