I'm aware of the conceptual differences between Aggregation and Composition. Can someone tell me the implementation difference in Java between them with examples?
-
3Follow this link you may get answer to your posts [Difference between Aggregation and Composition] [1] [1]: http://stackoverflow.com/a/1468285/1353243 – thar45 Aug 09 '12 at 10:56
-
possible duplicate of [Aggregation versus Composition](http://stackoverflow.com/questions/734891/aggregation-versus-composition) – Alex K Aug 10 '12 at 06:26
-
Also see [c-sharp-code-for-association-aggregation-composition](http://stackoverflow.com/questions/12604031/c-sharp-code-for-association-aggregation-composition) – nawfal Oct 03 '13 at 19:39
-
When we have any relation between objects, that is called Association. Aggregation and Composition both are specialized form of Association. Composition is again specialize form of Aggregation. http://www.javabench.in/2011/08/difference-between-association.html – Raúl May 28 '14 at 03:33
-
you can find more answers [here](https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition) – Dec 04 '19 at 05:29
9 Answers
Composition
final class Car {
private final Engine engine;
Car(EngineSpecs specs) {
engine = new Engine(specs);
}
void move() {
engine.work();
}
}
Aggregation
final 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.

- 20,708
- 48
- 131
- 198
-
10Great example! It also shows composition as strong association (a Car makes no sense without an Engine) and aggregation as a weak association (a Car without an Engine makes totally sense, it doesn't even need one in its constructor). Which one to use? Depends on the context. – Federico Pugnali Oct 26 '12 at 19:31
-
@Anand the example u give in Aggregation isn't this dependency example ? Dependency is a weaker form of relationship and in code terms indicates that a class uses another by parameter or return type. – OOkhan Nov 08 '13 at 11:37
-
@Anand: can you explain more why you said: in the case of composition there is no way for the outside world to get a reference to the Engine, with aggregation the outside world can have a reference to the Engine? Can you show in code sample, how the outside world can or cannot have a reference to the engine? thanks – O Connor Apr 02 '15 at 11:48
-
11This is not a correct example. The outside world can have access to the inner object but it's identity is always associated with the outer object while in aggregation the inner object may exist independently even if there was no car. In this case an engine can still be created using the `new Engine(EngineSpecs)` call even if there was no car. The way to achieve composition is to create Engine as an inner class, so that an object of engine is always created with reference to a Car Object – mickeymoon Feb 04 '16 at 21:41
-
1@mickeymoon great catch. can you point us to a better example? – Gayan Weerakutti Nov 12 '18 at 16:55
-
@GayanWeerakutti late to the party but I think this is a better example of composition https://stackoverflow.com/a/48338461/6120066 – OutsideCoder Mar 15 '20 at 15:34
-
I think your example about Composition is just a special case. @mickeymoon is right. – emeraldhieu Oct 06 '22 at 07:32
I would use a nice UML example.
Take a university that 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 respective departments disappear.
In java :
public class University {
private List<Department> 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<Professor> 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<Department> attachedDepartments;
public void destroy(){
}
public void fire(Department d){
attachedDepartments.remove(d);
}
}
Something around this.
EDIT: an example as requested
public class Test
{
public static void main(String[] args)
{
University university = new University();
//the department only exists in the university
Department dep = university.createDepartment();
// the professor exists outside the university
Professor prof = new Professor("Raoul");
System.out.println(university.toString());
System.out.println(prof.toString());
dep.assign(prof);
System.out.println(university.toString());
System.out.println(prof.toString());
dep.destroy();
System.out.println(university.toString());
System.out.println(prof.toString());
}
}
University class
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class University {
private List<Department> departments = new ArrayList<>();
public Department createDepartment() {
final Department dep = new Department(this, "Math");
departments.add(dep);
return dep;
}
public void destroy() {
System.out.println("Destroying university");
//it's composition, when I destroy a university I also destroy the departments. they cant live outside my university instance
if (departments != null)
departments.forEach(Department::destroy);
departments = null;
}
@Override
public String toString() {
return "University{\n" +
"departments=\n" + departments.stream().map(Department::toString).collect(Collectors.joining("\n")) +
"\n}";
}
}
Department class
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Department {
private final String name;
private List<Professor> professors = new ArrayList<>();
private final University university;
public Department(University univ, String name) {
this.university = univ;
this.name = name;
//check here univ not null throw whatever depending on your needs
}
public void assign(Professor p) {
//maybe use a Set here
System.out.println("Department hiring " + p.getName());
professors.add(p);
p.join(this);
}
public void fire(Professor p) {
//maybe use a Set here
System.out.println("Department firing " + p.getName());
professors.remove(p);
p.quit(this);
}
public void destroy() {
//It's aggregation here, we just tell the professor they are fired but they can still keep living
System.out.println("Destroying department");
professors.forEach(professor -> professor.quit(this));
professors = null;
}
@Override
public String toString() {
return professors == null
? "Department " + name + " doesn't exists anymore"
: "Department " + name + "{\n" +
"professors=" + professors.stream().map(Professor::toString).collect(Collectors.joining("\n")) +
"\n}";
}
}
Professor class
import java.util.ArrayList;
import java.util.List;
public class Professor {
private final String name;
private final List<Department> attachedDepartments = new ArrayList<>();
public Professor(String name) {
this.name = name;
}
public void destroy() {
}
public void join(Department d) {
attachedDepartments.add(d);
}
public void quit(Department d) {
attachedDepartments.remove(d);
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Professor " + name + " working for " + attachedDepartments.size() + " department(s)\n";
}
}
The implementation is debatable as it depends on how you need to handle creation, hiring deletion etc. Unrelevant for the OP

- 6,091
- 2
- 30
- 47
-
i hope i won't get any comment about not initilized lists and no constructors. I wrote this quickly, the missing parts are common sense but if asked i will complete the solution – TecHunter Aug 09 '12 at 15:38
-
Thanks! Your example is perfectly clear. But I could not understand your code illustration. Can you tell me the _basic_ implementation difference between the two? If I have to implement Aggregation or composition, what concepts in Java should I use? – Rajath Aug 10 '12 at 03:22
-
it's exactly the same implementation if you talk about class BUT composition should be reflected by the way you manage instances like in my edit – TecHunter Aug 10 '12 at 07:54
-
TecHunter, I was looking for this example. Can you also widen the java code for demonstrating association in University example. How should the example run in a main() method. ( which could show scenarios of creating University and deleting professors ) . Please help – deepakl.2000 Sep 01 '20 at 14:53
-
@deepakl.2000 many ways to do this. You should define which class has the power. it could be Professor joining a University then one to many department or it could be the university hiring a professor then asking the department to enroll. it's all about what makes more sense to your needs. – TecHunter Sep 01 '20 at 18:52
-
TecHunter Can you please post the complete Java code solution with the main() method . Please help. – deepakl.2000 Sep 02 '20 at 10:56
-
A simple Composition program
public class Person {
private double salary;
private String name;
private Birthday bday;
public Person(int y,int m,int d,String name){
bday=new Birthday(y, m, d);
this.name=name;
}
public double getSalary() {
return salary;
}
public String getName() {
return name;
}
public Birthday getBday() {
return bday;
}
///////////////////////////////inner class///////////////////////
private class Birthday{
int year,month,day;
public Birthday(int y,int m,int d){
year=y;
month=m;
day=d;
}
public String toString(){
return String.format("%s-%s-%s", year,month,day);
}
}
//////////////////////////////////////////////////////////////////
}
public class CompositionTst {
public static void main(String[] args) {
// TODO code application logic here
Person person=new Person(2001, 11, 29, "Thilina");
System.out.println("Name : "+person.getName());
System.out.println("Birthday : "+person.getBday());
//The below object cannot be created. A bithday cannot exixts without a Person
//Birthday bday=new Birthday(1988,11,10);
}
}

- 96
- 2
- 7
-
Can you also please add working java code for Association and Aggregation as well explaining all scenarios as you have explained in Composition ? 1. Scenarios in Association if they are deleted ? 2. Scenarios in Aggregation when parent object is deleted ? – deepakl.2000 Sep 01 '20 at 17:17
In simple terms :
Both Composition and Aggregation are Associations. Composition -> Strong Has-A relationship Aggregation -> Weak Has-A relationship.

- 522
- 5
- 15
There is a great explanation in the given url below.
http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit
Please check!!!

- 687
- 1
- 10
- 8
-
Hi Rahul, while this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. Please take a look here: [Why and how are some answers deleted?](http://stackoverflow.com/help/deleted-answers) – bummi Aug 02 '15 at 07:03
First we must talk about what actually the difference between Aggregation
and Composition
is to be on the same page.
Aggregation is an association where the associated entity may exist independent of the association. For example, a Person may be associated to an Organisation but he/she may have independent existence in the system.
whereas
Composition refers to a situation when one of the associated entities is strongly related to the other and cannot exist without the other's existence. In fact the identity of that entity is always associated with the identity of the other object. For example, wheels in a car.
Now, aggregation can simply be achieved by holding a property of one entity in another as below:
class Person {
Organisation worksFor;
}
class Organisation {
String name;
}
class Main {
public static void main(String args[]) {
//Create Person object independently
Person p = new Person();
//Create the Organisation independently
Organisation o = new Organisation();
o.name = "XYZ Corporation";
/*
At this point both person and organisation
exist without any association
*/
p.worksFor = o;
}
}
For Composition it is necessary that the dependent object is always created with the identity of its associated object. You can use an inner class for the same.
class Car {
class Wheel {
Car associatedWith;
}
}
class Main {
public static void main() {
//Create Car object independently
Car car = new Car();
//Cannot create Wheel instance independently
//need a reference of a Car for the same.
Car.Wheel wheel = car.new Wheel();
}
}
Please note that the same use case may fall under aggregation/composition depending on the application scenario. For example, the Person-Organisation case may become composition if you are developing an application for people working in some organisation and the reference to organisation is must for sign up. Similarly, if you are maintaining inventory for parts of a Car, Car-Wheel relationship can be aggregation.

- 4,820
- 5
- 31
- 56
The difference is that any composition is an aggregation and not vice versa.
Let's set the terms. The Aggregation is a metaterm in the UML standard, and means BOTH composition and shared aggregation, simply named shared. Too often it is named incorrectly "aggregation". It is BAD, for composition is an aggregation, too. As I understand, you mean "shared".
Further from UML standard:
composite - Indicates that the property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (parts).
So, University to cathedras association is a composition, because cathedra doesn't exist out of University (IMHO)
Precise semantics of shared aggregation varies by application area and modeler.
I.e., all other associations can be drawn as shared aggregations, if you are only following to some principles of yours or of somebody else. Also look here.
Aggregation vs Composition
Aggregation implies a relationship where the child can exist independently of the parent. For example, Bank and Employee, delete the Bank and the Employee still exist.
whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a Human.
Aggregation relation is “has-a” and composition is “part-of” relation.
Composition is a strong Association whereas Aggregation is a weak Association.

- 477
- 5
- 17
-
1Bank and Employee should be a composition relationship. Because if you delete the bank, the employee does not continue to exist. We must think in terms of the system, not real world. Yes, the person continues to exist. But if a bank goes totally bankrupt, the employees would be terminated - you cannot be an employee at a non-existent bank. A better example would be BankBranch and Customer. If you delete a Bank's branch (it goes under) the customer still exists and has the option of being assigned to a different branch. And organs can exist separate a human in the context of transplants/labs. – Boris Jul 12 '21 at 00:13
Both types are of course associations, and not really mapped strictly to language elements like that. The difference is in the purpose, context, and how the system is modeled.
As a practical example, compare two different types of systems with similar entities:
A car registration system that primarily keep track of cars, and their owners, etc. Here we are not interested in the engine as a separate entity, but we may still have engine related attributes, like power, and type of fuel. Here the Engine may be a composite part of the car entity.
A car service shop management system that manages car parts, servicing cars, and replace parts, maybe complete engines. Here we may even have engines stocked and need to keep track of them and other parts separately and independent of the cars. Here the Engine may be an aggregated part of the car entity.
How you implement this in your language is of minor concern since at that level things like readability is much more important.

- 564
- 3
- 10