Composition and Aggregation in Java
In Java, composition and aggregation define relationships between classes, allowing us to model real-world scenarios by linking objects within a program. These relationships enable classes to work together without being hierarchically dependent on each other, unlike inheritance. Here, we’ll explain these relationships with examples and scenarios for clear understanding. You can visit the detailed tutorial here.
1. Composition in Java
Contents
Composition represents a strong relationship between two classes. If an object (child) is a part of another object (parent), and cannot exist independently, this relationship is known as composition. The lifecycle of the contained (child) object is bound to the lifecycle of the container (parent) object. In simpler terms, if the parent object is destroyed, the child object is also destroyed.
Example: Car
and Engine
Let’s consider a Car
class that has an Engine
. Here, a car cannot function without an engine, and if the car is destroyed, the engine is as well. This scenario makes Engine
a part of Car
, demonstrating a “has-a” relationship.
Code Example:
// Engine class representing the part of a Car
class Engine {
private String type;
public Engine(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void start() {
System.out.println("Engine of type " + type + " is starting.");
}
}
// Car class that uses Composition
class Car {
private Engine engine; // Car "has-an" Engine
public Car(String engineType) {
this.engine = new Engine(engineType); // Composition: Engine is created and owned by Car
}
public void startCar() {
System.out.println("Car is starting.");
engine.start(); // Car depends on Engine's functionality
}
public void showDetails() {
System.out.println("Car with engine type: " + engine.getType());
}
}
public class CompositionDemo {
public static void main(String[] args) {
Car car = new Car("V8");
car.startCar();
car.showDetails();
// Output:
// Car is starting.
// Engine of type V8 is starting.
// Car with engine type: V8
}
}
Explanation:
- In this example,
Car
contains anEngine
object. TheEngine
is a part ofCar
, and it is created inside theCar
constructor. This means that withoutCar
, there is no independentEngine
. - This relationship is composition because the
Engine
is entirely dependent on the existence ofCar
.
2. Aggregation in Java
Aggregation represents a weaker relationship than composition. In aggregation, the contained (child) object can exist independently of the container (parent) object. In this case, the objects have their own lifecycles, and the destruction of one does not affect the existence of the other.
Example: House
and Person
Imagine a scenario where a House
contains a list of Person
objects as its residents. Even if the House
object is destroyed, the Person
objects (people) still exist. This relationship reflects aggregation, as House
aggregates Person
objects, but they are not exclusively dependent on each other.
Code Example:
import java.util.ArrayList;
import java.util.List;
// Person class representing individual residents
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
// House class using Aggregation
class House {
private List<Person> residents; // House "has" residents
public House() {
this.residents = new ArrayList<>();
}
public void addResident(Person person) {
residents.add(person); // Aggregation: House contains references to Person objects
}
public void showResidents() {
System.out.println("House residents:");
for (Person person : residents) {
System.out.println(person.getName());
}
}
}
public class AggregationDemo {
public static void main(String[] args) {
// Creating Person objects independently
Person person1 = new Person("Alice");
Person person2 = new Person("Bob");
// Creating House and adding residents
House house = new House();
house.addResident(person1);
house.addResident(person2);
house.showResidents();
// Output:
// House residents:
// Alice
// Bob
}
}
Explanation:
- The
House
class has a list ofPerson
objects, but thesePerson
instances are created independently and are not owned by theHouse
. - If
House
is destroyed, thePerson
objects (people) still exist. This represents an aggregation relationship, whereHouse
andPerson
objects are loosely coupled and can exist independently.
Comparison of Composition and Aggregation
Aspect | Composition | Aggregation |
---|---|---|
Relationship | Strong (“part-of”) | Weak (“has-a”) |
Lifetime Dependency | Dependent: child cannot exist without parent | Independent: child can exist without parent |
Example | Car and Engine | House and Person |
Implementation | Contained object created within the parent class | Contained object passed from outside |
When to Use Composition vs. Aggregation
- Use composition when the contained object is a part of the parent object and cannot exist independently. Examples:
Car
andEngine
,Body
andHeart
. - Use aggregation when the contained object can exist independently of the parent object. Examples:
House
andPerson
,Library
andBook
.
Composition and aggregation are essential in creating flexible and reusable class structures in object-oriented programming, allowing for modular designs that can represent complex relationships.
14 thoughts on “Composition and Aggregation in Java”
Beylikdüzü su kaçak tespiti Evimdeki su kaçağı sorununu hızlıca çözdüler, gerçekten profesyonel bir ekip! https://www.myidsocial.com/1670674343844667_1815
İstanbul su kaçağı tespiti hizmeti Kalamış su kaçağı tespiti: Kalamış’ta su kaçaklarına teknolojik çözümler. https://socialsnug.net/ustaelektrikci
Büyükçekmece su kaçağı tespiti Acıbadem su kaçağı tespiti: Acıbadem’de su kaçağı sorunlarınıza kalıcı çözümler. https://trngamers.co.uk/ustaelektrikci
Alemdağ su kaçak tespiti Duvarlar kırılmadan çözüm sunmaları evdeki tadilat masrafını da azalttı. https://www.hockeynhlforum.com/ustaelektrikci
Üsküdar su kaçağı tespiti Eyüp su kaçağı tespiti: Eyüp’te su kaçaklarını teknolojik cihazlarla buluyoruz. https://hasanonen.av.tr/Soru/su-tesisatcisi-gorevleri
Beşiktaş su kaçağı tespiti Levent su kaçağı tespiti: Levent’te su kaçağını en hızlı ve doğru şekilde tespit ediyoruz. https://thehealthbridges.com/ustaelektrikci
Taşoluk su kaçak tespiti Moda su kaçağı tespiti: Moda’da su kaçağı sorunlarınıza hızlı çözüm. https://goodandbadpeople.com/ustaelektrikci
BWER empowers businesses in Iraq with cutting-edge weighbridge systems, ensuring accurate load management, enhanced safety, and compliance with industry standards.
ocenili váš obsah. Dejte mi prosím vědět.
reading this weblog’s post to be updated daily.
Muito obrigado!}
díky tomuto nádhernému čtení! Rozhodně se mi líbil každý kousek z toho a já
) Vou voltar a visitá-lo uma vez que o marquei no livro. O dinheiro e a liberdade são a melhor forma de mudar, que sejas rico e continues a orientar os outros.
e dizer que gosto muito de ler os vossos blogues.