-1

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.

public class Person {

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

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



    public Address getAddress() {
            return address;
    }


}

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

}
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
hewa jalal
  • 952
  • 11
  • 24
  • 1
    Maybe it's better for you to understand the conceptual difference between composition and aggregation. See https://stackoverflow.com/questions/20304116/aggregation-and-composition-in-java-code for example. – rafaelim Apr 23 '19 at 18:35
  • @rafaelim i already checked that but it didn't helped. – hewa jalal Apr 23 '19 at 18:36
  • @rafaelim i just don't understand whyif we made Person object=null, the Address Object will be null too. – hewa jalal Apr 23 '19 at 18:42
  • I see. One way to see difference between composition and aggregation in a java class, is to see if the object is required or not. Composition, by definition, the associated objects need to coexist. Aggregation in other hand, the object associated to a class is not required. – rafaelim Apr 23 '19 at 18:44
  • thanks but i know the theory behind its i just don't how it works, as i mentioned with the null example above. – hewa jalal Apr 23 '19 at 18:49
  • Maybe you're focusing too much on code. This concepts is more related do domain you're working than code itself. In your example, if Person and Address is a composition, then an object of Person cannot be created without the Address object. This is a composition. This is the important part. In this code: Person object = null, you're just losing the reference to the object. You're not doing anything with Address object inside the Person object. – rafaelim Apr 23 '19 at 19:07
  • @rafaelim Okay thanks. – hewa jalal Apr 23 '19 at 19:19
  • @hiwadoski See my answer maybe it helps understand both concepts of Aggregation and Composition. – Oussama Ben Ghorbel Apr 23 '19 at 20:34
  • @OussamaBenGhorbel Thanks for the answer. – hewa jalal Apr 24 '19 at 12:03

1 Answers1

1

Well, let me first start by explaining the differences between Aggregation and Composition.

Aggregation: 1. A child in an Aggregation relationship can exist independently of the parent 2. Aggregation can be read as "HAS A"

Example: If we have a class Wheel, the relationship between the class Wheel and Car is an Aggregation: "A car has four wheels". A Wheel can exist out of the context of a car.

Composition: 1. A child in an Composition relationship cannot exist independently of the parent. In other words, there's no sense of for the child class to exist if it cannot be hosted into the parent class. 2. Composition can be read as IS PART OF

Example: If we have a class Human, the relationship between the class Heart and Human is composition. You never seen a heart hanging out by itself, don't you? Hence, the need for Composition.


In Java we represent the Composition relationship using private final

class Human { private final Heart heart; }

While Aggregation would be just:

class Wheel { ... }
class Car { private List<Wheel> wheels; }

Now let's say, we would like to remove an object of class Human (taking into account he is not an organ donor) completely, the Composition relationship forces us to destroy the instance of the heart related to that human being. In Aggregation it is not the case, the wheels can be used by another car if the instance of the car they used to belong to got wrecked.

I hope this helps.

Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34