UML Association vs Aggregation vs Composition - Javatpoint.
Composition allows creation of back-end class when it’s needed, for example we can change Person getSalary method to initialize the Job object at runtime when required. Further Reading: Do you know one of the best practice in java programming is to use composition over inheritance, check out this post for detailed analysis of Composition vs Inheritance.
For example: A car has a engine, a window has a button, a zoo has a tiger. Composition is a special case of aggregation. In other words, a restricted aggregation is called composition. When an object contains the other object and the contained object cannot exist without the other object, then it is called composition.
Aggregation is not aggravation. In Java it means that a relationship exists between classes, but it's only one-way. This lesson will highlight the concept and provide working code examples.
Hence you must know about the relationships between two objects, like one-to-one, many-to-one, one-to-many and many-to-many as all these are the association between objects. Association also has its special form with Aggregation and Composition is the special form of aggregation. For Example, Organization and Employee have an association. (java).
Aggregation (HAS-A relationship) in Java. Aggregation is a term which is used to refer one way relationship between two objects. For example, Student class can have reference of Address class but vice versa does not make sense. In Java, aggregation represents HAS-A relationship, which means when a class contains reference of another class known to have aggregation.
Note: -The scope of this article is only limited to aggregation, association and composition. So we will not discuss inheritancein this article as its pretty straight forward and I am sure you can get1000 of articles on the net which will help you in understanding the same.
Inheritance is suitable only when classes are in a relationship in which subclass is a (kind of) superclass.For example: A Car is a Vehicle so the class Car has all the features of class Vehicle in addition to the features of its own class.However, we cannot always have. is a relationship between objects of different classes.