Aggregation, Association and Composition (examples of code given)
Posted
by
Bukocen
on Stack Overflow
See other posts from Stack Overflow
or by Bukocen
Published on 2010-12-01T13:15:08Z
Indexed on
2011/11/14
9:51 UTC
Read the original article
Hit count: 155
I have such a simple example:
public class Order
{
private ArrayList<Product> orders = new ArrayList<Product>();
public void add(Product p)
{
orders.add(p);
}
}
Is it aggregation or composition? I guess it's composition, because orders will be delated after delete of Order, right? Unfortunately it was a task and answer was different;/ Do you know why?
second problem:
public class Client extends Person
{
String adress = "";
Orders orders = new Orders();
public Client(String n, String sn)
{
name = n;
surName = sn;
}
public String getAddress()
{
return adress;
}
public Orders getOrders()
{
return this.orders;
}
}
Is it Association between Client and Orders? My teacher told me that this is association, but I was wondering why it's not a aggregation/composition - he told me that aggregation or composition occur only when one class contains few instances of different class - is that right? I guess not, because e.g. car contains ONE wheel and it's aggregation I guess? What type of relation is that and why?
Greetings
© Stack Overflow or respective owner