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?
1
- Aggregation is a Whole-Part relationship. And so it is also a has-a relationship.
- By definition the part objects can have life cycles of their own, independent of the “whole” object.
- 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.
- A is wrong because this is either inheritance or composition. Depends on how technically we read “sub class.” Either way, its not aggregation.
- B is wrong, that is polymorphism, implying the idea of coding to interfaces
- 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.
- D is the best answer.
- E is wrong, it simply does not meet the definition of aggregation.
Hmm, Based on this wiki, I would go with D.
It seems that the intent was to ‘Combining several classes to create a new class’, means, to include several classes.
Clarification:
I am not sure though. Its confusing because the Answer could be read as the opposite of sub-classing. Where one takes a bunch of sub classes and creates one monster object with all its properties. In which case all the properties are lost on the objects destruction.
My quick read:
- A: Sounds like Sub-classing
- B: Sounds like Polymorphism or something similar
- C: Sounds like property
- D:
- E: Sounds like inheritance
1
I think the answer they want is “D – Combining several classes to create a new class.”
Aggregate means group together;
- A1
- A2
- B1
Aggregated;
- A
- 1
- 2
- B
- 1