+ 3
What does aggregation means and mention it's use also?
In Java inheritance
6 Réponses
+ 8
Edwin I have to agree with ~ swim ~. Your 2nd paragraph inaccurately defines composition as an "is-a" relationship implemented via inheritance.
However, composition is actually similar to aggregation as they are both defined with the "has a" relationships and are both types of associations between objects.
Aggregations relate independent entities. Ex. A Student can be related to zero or more Courses. A Course can be related to zero or more Students.
A Course can exist prior to being associated with Students. Students can exist prior to associating with Courses.
These relationships involve "aggregating" different entities.
Next, I'll describe Composition.
+ 8
Composition relates Class A with Class B such that Class B is dependent on Class A and cannot exist without Class A.
Examples of Composition:
- Student -> CourseEnrollment
- Customer -> Order
- Course -> Module
In the above examples, the class types on the right could not exist without the class types on the left.
+ 2
An aggregation -has-a relationship, e. g. a person has an address.
You implement that by a reference from one class (Person) to another class (Address) so that Address is an attribute of Person. See:
http://www.javatpoint.com/aggregation-in-java
A composition is defined as an is-a relationship, e.g. a person is a programmer. That can be implemented as an inheritance (Programmer extends Person).