Association, Composition & Aggregation
Association, Aggregation and Composition are terms that represent relationships among objects. They are very basic stuff of Object-Oriented Programming.
In this article, I’ll explain association, composition & aggregation. Let’s get started:
Table of Contents
Association

Association is a relationship between two objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many. Composition and aggregation are two types of association.
Example: A Car and a Engine are having an association.
Composition

Composition is a strong association. An association is said to composition if an object owns another object and another object cannot exist without the owner object.
Example: Car class contains Engine. Engine cannot exist without a Car. There exists composition between Car and Engine.
// Car must have Engine
public class Car {
// engine is a mandatory part of the car
private final Engine engine;
Car() {
engine = new Engine();
}
}
// Engine Object
class Engine {}
Aggregation

Aggregation is a weak association. An association is said to be aggregation if both objects can exist independently.
Example: A Team has 0 or more players. A Player can be in a Team or not. There exists aggregation between Team and Player.
// Team
public class Team {
// players can be 0 or more
private List players;
Team() {
players = new Player();
}
}
// Player Object
class Player {}
The article is over. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)