4

Please excuse the poor example/analogy, I'm only interested in the code sample.

I have a Dinner_Chair class (inherited from Chair class). It is as follows.

Dinner_Chair = class(Chair)
    Private
      theUser: Person;

    Public
      Dinner_Chair()
      {
            Back = new Back();
            Seat = new Seat();
      }
End

I also have a simple Person class, that as you can see, is associated with the Dinner_Chair class.

My question is this. Because the Person class is not instantiated in the Dinner_Chair class, is this an example of aggregation?

This is to consolidate my understanding of entry level OOP relationships.

1 Answers1

2

In code terms it could be aggregation but not necessarily. In the real world of dining chairs and people/diners, the relationship isn't aggregation because both objects can exist independently of each other.

The differentiation with regards to the code sample is whether a Dinner_Chair is a valid object without a Person.

Aggregation implies the Person is a required dependency of Dinner_Chair - i.e. Dinner_Chair isn't valid without a Person.

Association implies the Person is an optional dependency of Dinner_Chair - i.e. Dinner_Chair is valid without a Person.

Ben Cottrell
  • 12,133