Often, I here about an interview question being asked that is “What is composition and aggregation ?” Or “What is the difference between composition and aggregation ?”
And trust me if you don’t know already, a lot of people (sometimes experienced developers) get stuck on this question.
Both, Composition and Aggregation, are actually kinds of Association.
Association – A teasing word, I know 😉
As shown from its meaning, Association is a connection between any two or more things. We will not go into the detail of association, Just know that it is
simply a connection between two or more things
So repeat with me,
Association is a connection between two or more things.
Once more, with feeling…
Association is a connection between two or more things.
Remember it, I am going to bring it forward in the coming lines.
Now, let me give you the definition of composition and aggregation in two simple lines.
Composition : A connection or link between two objects where one object CANNOT exist without the other.
Aggregation : A connection or link between two objects where one object CAN exist without the other.
Example 1 – A clear example
Taken, a university has 1 to 20 different departments and each department has 1 to 5 professors. There is a composition link between a University and its’ departments. There is an aggregation link between a department and its’ professors.
Composition is just a STRONG aggregation, if the university is destroyed then the departments should also be destroyed.
But we shouldn’t kill the professors even if their respectives departments disappear.
A picture is worth a thousand words
[one_half]

[/one_half]
[one_half_last]

[/one_half_last]
And the relationship is
Code Example
public class University { private List departments; public void destroy(){ //it's composition, when i destroy a university I also destroy the departments. they cant live outside my university instance if(departments!=null) for(Department d : departments) d.destroy(); departments.clean(); departments = null; } } public class Department { private List professors; private University university; Department(University univ){ this.university = univ; //check here univ not null throw whatever depending on your needs } public void destroy(){ //It's aggregation here, we just tell the professor they are fired but they can still keep living for(Professor p:professors) p.fire(this); professors.clean(); professors = null; } } public class Professor { private String name; private List attachedDepartments; public void destroy(){ } public void fire(Department d){ attachedDepartments.remove(d); } }
Example 2
[one_half] [/one_half] [one_half_last]
[/one_half_last]
Composition : If a car CANNOT run without an engine, that is composition. The car is composed of engine. Engine is an internal part of the car. Engine cannot exist without the car.
Aggregation : If a car CAN run without an engine, that is aggregation. And believe me, there are cars which can run without an engine. So here, engine is not ALWAYS an internal part of the car.
Code example
Composition
class Car { final Engine engine; Car(EngineSpecs specs) { engine = new Engine(specs); } void move() { engine.work(); } }
Aggregation
class Car { private Engine engine; void setEngine(Engine engine) { this.engine = engine; } void move() { if (engine != null) engine.work(); } }
In the case of composition, the Engine is completely encapsulated by the Car. There is no way for the outside world to get a reference to the Engine. The Engine lives and dies with the car. With aggregation, the Car also performs its functions through an Engine, but the Engine is not always an internal part of the Car. Engines may be swapped, or even completely removed. Not only that, but the outside world can still have a reference to the Engine, and tinker with it regardless of whether it’s in the Car.
If this article helped you, return the help by sharing 🙂