Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what makes composition different from aggregation they are both declared as private, is it in composition example we are creating an object of Address class in constructor of Person, if it's then how this makes the Person object control/own the Address object.

Composition Example:
Java
public class Person {

private String firstName;
private String lastName;
private Address address;

//Composition example
public Person() {
    address = new Address();
}



public Address getAddress() {
    return address;
}


}


Aggregation example

Java
Employee Class

public class Employee {

private String firstName;
private String lastName;
private int age;

//Aggregation Java example
private Address address;


public void setAddress(Address address) {
    this.address = address;
}
public Address getAddress() {
    return address;
}

}


What I have tried:

there was nothing i could try.
Posted
Updated 24-Apr-19 0:46am

 
Share this answer
 
Comments
hiwa doski 24-Apr-19 10:21am    
Okay thank you.
Maciej Los 24-Apr-19 14:29pm    
You're very welcome.
Both are "aggregation" (an address and an employee can exist independently); the second also shows "injection".

https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-aggregation-vs-composition/
 
Share this answer
 
Comments
hiwa doski 24-Apr-19 10:21am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900