educative.io

Educative

The difference of implementation of composition and aggregation?

Not fully understanding the difference of how we implement the composition and aggregation…
So is it correct, that when we are just using the method of other object in our class - as was done in topic with Aggregation - we are using the Has-A approach and there is no connection with life expectancy of the objects, but when we are creating a new objects of other class inside of out class - it is a Part-Of and there is a connection?
It that approach in thinking right? :slight_smile:

Thanks!
Dmitry

Type your question above this line.

Course: https://www.educative.io/collection/10370001/6201068373409792
Lesson: https://www.educative.io/collection/page/10370001/6201068373409792/4660708266475520

Hi @Dmitry_Polovinkin

Yes, you’re right. In aggregation, there’s a weak relationship between two classes and that leads to independency, whereas in composition, there’s a strong relationship. One class cannot live without the other class.

Feel free to drop in more queries.
Thanks.

Hi, thanks for answering, but I kinda understand it already… :slight_smile:
As the title of question says - it’s more about how those two approaches are implemented , that’s what wasn’t described in the topic enough

1 Like

In the sample code snippet, I added a few lines to delete the car object but the other objects that are part of a car still live after the deletion of car object. Could you explain to me why that is the case?

car = Car(1600, 4, 2, “Grey”)
car.printDetails()
engine = car.eObj
tires = car.tObj
doors = car.dObj

del car

engine.printDetails()
tires.printDetails()
doors.printDetails()