Which Closely Represents Aggregation? – Object-Oriented Design

designobject-oriented

I know Aggregation is a has-a relationship, but I encountered a question in a test which did not make sense (and had grammatical mistakes as well)

Which of the following statements correctly describe the concept of aggregation in OOP?

  • A Splitting a class into a several sub classes
  • B Creating one piece of code that works with all similar objects
  • C Accessing data only through methods
  • D Combining several classes to create a new class
  • E Creating a new class from an existing class

I think;

  • A Could be true.
  • B Sounds like inheritance.
  • C Seems like property.
  • D Could be true.
  • E Could be true.

I'm uncertain how the has-a relationship translates into actual code in these statements. Any ideas?

Best Answer

  1. Aggregation is a Whole-Part relationship. And so it is also a has-a relationship.
  2. By definition the part objects can have life cycles of their own, independent of the "whole" object.
  3. Composition is aggregation with a twist. It adds the idea that the constituent parts do not/cannot/should not exist without the whole. The parts have no real meaning or functionality outside the whole. And conversely the whole is incomplete or cannot function properly without its parts.
  4. A is wrong because this is either inheritance or composition. Depends on how technically we read "sub class." Either way, its not aggregation.
  5. B is wrong, that is polymorphism, implying the idea of coding to interfaces
  6. C is wrong, that is encapsulation; in its pure form I'd say. Using a class via what it does - methods - vice accessing its state properties.
  7. D is the best answer.
  8. E is wrong, it simply does not meet the definition of aggregation.
Related Topic