I was very bored at home yesterday, wanted to engage with something new. Suddenly thought about the old carom board and I searched for it in the store room.I found the set of coins but the board was damaged.
Is there any significance for that coins without that carom board?
No!
Which means these two have a special relationship.
The objects that we see around us may have a relationship. It can be one-to-one, one-to-many, many-to-one, and many-to-many.
Association signifies these kind of relationship between multiple objects and Aggregation and Composition are the two forms of Association.
Coins cannot logically exist without the carom board.This kind of relationship or association is called ‘Composition’. i.e. If one class owns the other class then the other class cannot meaningfully exist when it’s owner is destroyed.
For example plant and leaf; human and heart.
- How it can be implemented in java ?
class SIM {
private String serviceProvider;
private String type;
public String getServiceProvider() {
return serviceProvider;
}
public void set ServiceProvider(String serviceProvider) {
this.serviceProvider = serviceProvider;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
class Mobile {
private final SIM sim;
public Mobile() {
sim = new SIM();
}
}
public class Test {
public static void main(String[] args) {
Mobile mobile = new Mobile();
}
}
Here when I create an object of Mobile, SIM object is automatically created. If I destroy the Mobile object then the SIM object has no significance in this scenario.
Now let’s look into the other type of association called Aggregation:
Aggregation is a kind of association where one class owns other class then the other class can exist even if the owner is destroyed.
For example College and Student. Even if College is closed the Student still exist.
So in the case of Aggregation, if the owner object is destroyed it does not really affect the associated object.
- How it can be implemented in java ?
class Car {
private String model;
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
class Driver {
private String name;
private int age;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
public class TestDriver {
public static void main(String[] args) {
Driver driver = new Driver();
driver.setName(“David”);
Car car = newCar();
car.setModel(“Toyota Camry”);
driver.setCar(new Car());
}
}
Here I have a class Driver and Driver has a Car(which means class Driver has a reference of another class Car). I created an object of class Driver and initialized its variables (including Car). Now if I destroy the Driver object, it does not really affect the Car which is the associated object. i.e. still the Car object has its significance.
UML notation for these kinds of relations are shown below:
For the example what I have mentioned above – the UML diagram looks like this.
Conclusion:
Composition is an association in which one class belongs to a group or collection.
This is a part of a whole relationship where a part cannot exist without a whole.
Therefore if a whole is deleted then all parts are also deleted.
If we can consider an order is a whole and line items are parts, then if the order is deleted then all corresponding line items for that order should be deleted.
So composition has a stronger relationship.
Aggregation is an association in which one class belongs to a group or collection. This is a part of a whole relationship where a part can exist without a whole.If we take a line item as a whole and product as a part, then if the line item is deleted then corresponding product doesn’t have to be deleted.
So aggregation has a weaker relationship.
—Amrutha B
About the author: Amrutha is working as a Junior Consultant in jThread IT Training & Consultancy.
Excellent Ma’am. It is really helpful. Thank you.
Very well explained.. Great job.
Well explained and the examples are very good to understand the concept. Keep going!!!
Very well explained..Its really helpful..Thank you for the new information..
Great Job Ma’m .Your way of explanation is excellent.
Thank you so much.
Easy to understood.. Excellent work 👍
Very well explained.good job mam.
Very well explained.good job mam.
Brilliant explanation Mam.
Thanks a lot
Good words madam. Understood the terms composition and aggregation.
Nicely explained . Thank you mam.
Super clear
Thanks a lot
Great Job
Wonderful explanation.. Its really helpful for me.. Thank you Miss
I’ve been googling for hours and still didn’t understand the concept .
Now I got the clear picture about this .
Thank you so much madam.
Wow 🙂.No words to Say.
Really nice
Good explanation thanks
Well explained….Keep writing 😊😊…All the best👍👍
Great Job❤️
Good thought👍👍explained the concept in simple words..
Keep writing.! All the best😊
Very well explained simply and the explanation is more catching
Nice explanation
Good and clear explanation…Thank you 🙂
Well explained… Thank you…..