Dependency vs Composition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Dependency vs Composition

I'm confused between the differences in the two of them, as I see both of them naket an instance of another class inside them... So how are they different?

19th Sep 2021, 5:38 PM
Jace🎭
Jace🎭 - avatar
3 Answers
+ 2
The two are often very similar. The main difference you'll usually see is that with Dependency, the instance creation of the other class will take place outside of the encapsulating class and is passed in, usually via the classes constructor. With Composition, the instance of the other class is created within the encapsulating class itself, again usually in the constructor, but may also be done within a method or as a field.
19th Sep 2021, 6:34 PM
ChaoticDawg
ChaoticDawg - avatar
19th Sep 2021, 10:15 PM
ChaoticDawg
ChaoticDawg - avatar
0
ChaoticDawg but then dependency looks like an aggregation since you do the same thing you described for dependency
19th Sep 2021, 9:30 PM
Jace🎭
Jace🎭 - avatar