The following code is given:

class Warren
  private const int MaxRabbitsInWarren = 99;
  private Rabbit[] Rabbits;
  private int RabbitCount = 0;
  private int PeriodsRun = 0;
  private bool AlreadySpread = false;
  private int Variability;
  private static Random Rnd = new Random();

  public Warren(int Variability)
    this.Variability = Variability;
    Rabbits = new Rabbit[MaxRabbitsInWarren];
    RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
    for (int r = 0; r < RabbitCount; r++)
      Rabbits[r] = new Rabbit(Variability);
  • There is an array called Rabbits, of type Rabbit, which is a field of class Warren.
  • The Warren constructor creates a random number of ‘Rabbits’ to be placed in the Rabbits array.

Because the array Rabbits is a member of the Warren class, does this means that its lifecycle is tightly bound to the Warren class? (ie. destroying a warren, destroys the rabbit array?)

  • This isn't an example of composition but the lifecycle of the Rabbits array is bound to the Warren object it's instantiated in.
    – Alex
    Nov 24, 2016 at 11:20

1 Answer 1


Is this an example of composition?

Yes - a Warren "has an" array of Rabbits.

does this means that [the array of Rabbits'] lifecycle is tightly bound to the Warren class?

Not necessarily. The specifics may vary between languages (I'm assuming that your language has some kind of automatic garbage collection), but if the array is shared outside of the Warren instance (and a reference to it is kept) it would survive even after the Warren is destroyed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.