I have java code which implements aggregation however when UML diagram is generated in IntelliJ Idea, it is generated with a composition symbol (filled diamond).
Code is given below:
public class Address {
private String city;
private String state;
private String country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", state='" + state + '\'' +
", country='" + country + '\'' +
'}';
}
}
public class Employee {
private String name;
private int age;
private Address address;
public Employee(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}
public class Practice {
public static void main(String[] args) {
Address address = new Address("City", "State", "Country");
Employee employee = new Employee("Name",22, address);
System.out.println(employee);
employee = null;
System.out.println(employee);
System.out.println(address);
}
}
When code is run, it meets the requirements of aggregation concept.
My question(s) are:
- Does not my code implement aggregation correctly?
- If it implements correctly then why my UML diagram tool detects java code implementation as composition between Employee and Address?
I am looking forward for precise explanation.
Thanks