Skip to content

Commit 0a2d1e7

Browse files
authored
Merge pull request #124 from apurvatrpth/compositon-vs-aggregator
Add README.MD for AggregationVsComposition on code level
2 parents c9eae6f + f1ab766 commit 0a2d1e7

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Aggregation vs Composition in Java (Code-Focused)
2+
3+
In OOP, **Aggregation** and **Composition** both express "has-a" relationships, but they differ in **ownership**, **object lifecycle**, and **instantiation strategy**.
4+
5+
---
6+
7+
## 🔗 Aggregation — External Object Reference
8+
9+
### ✅ Key Code Characteristic:
10+
> The referenced object is **passed from outside** and can exist independently.
11+
12+
```java
13+
class Engine {
14+
String type;
15+
Engine(String type) {
16+
this.type = type;
17+
}
18+
}
19+
20+
class Car {
21+
Engine engine; // Aggregation - reference passed from outside
22+
23+
Car(Engine engine) {
24+
this.engine = engine;
25+
}
26+
27+
void showEngineType() {
28+
System.out.println("Engine: " + engine.type);
29+
}
30+
}
31+
32+
public class AggregationDemo {
33+
public static void main(String[] args) {
34+
Engine e1 = new Engine("V8"); // Created externally
35+
Car car1 = new Car(e1); // Shared with car1
36+
37+
Engine e2 = new Engine("V6");
38+
Car car2 = new Car(e2); // Shared with car2
39+
40+
car1.showEngineType(); // Engine: V8
41+
car2.showEngineType(); // Engine: V6
42+
}
43+
}
44+
```
45+
46+
**🧠 Key Point:**
47+
- `Car` does **not own** `Engine`.
48+
- The same `Engine` can be reused across multiple `Car` objects.
49+
- `Engine` can outlive or live independently of `Car`.
50+
51+
---
52+
53+
## 🔒 Composition — Object Created Internally
54+
55+
### ✅ Key Code Characteristic:
56+
> The referenced object is **created inside** the constructor. It **cannot exist without the parent.**
57+
58+
```java
59+
class Engine {
60+
String type;
61+
Engine(String type) {
62+
this.type = type;
63+
}
64+
}
65+
66+
class Car {
67+
private Engine engine; // Composition - created inside
68+
69+
Car(String engineType) {
70+
this.engine = new Engine(engineType); // tightly coupled
71+
}
72+
73+
void showEngineType() {
74+
System.out.println("Engine: " + engine.type);
75+
}
76+
}
77+
78+
public class CompositionDemo {
79+
public static void main(String[] args) {
80+
Car car = new Car("Electric"); // Engine is part of Car
81+
car.showEngineType(); // Engine: Electric
82+
}
83+
}
84+
```
85+
86+
**🧠 Key Point:**
87+
- `Car` **fully owns** `Engine`.
88+
- `Engine` cannot be reused or accessed independently.
89+
- Lifecycle of `Engine` is strictly bound to `Car`.
90+
91+
---
92+
93+
## 🔍 Code-Level Differences Summary
94+
95+
| Aspect | Aggregation | Composition |
96+
|------------------------|---------------------------------------------|--------------------------------------------|
97+
| Object creation | Object is passed from outside | Object is created internally |
98+
| Ownership | Parent has a reference | Parent owns the object |
99+
| Lifecycle dependency | Child can live without parent | Child dies with parent |
100+
| Reusability | Child can be reused by multiple parents | Child is bound to one parent only |
101+
| Code style | `this.child = child;` | `this.child = new Child();` or similar |
102+
103+
---
104+
105+
## 💡 When to Use
106+
107+
- **Use Aggregation** when you want to share or reuse the same instance.
108+
- **Use Composition** when you want a part to be a core, non-detachable component of the object.
109+
110+
---

0 commit comments

Comments
 (0)