What are Composition and Aggregation? State the difference.
Question Explanation
Composition and Aggregation are two fundamental concepts in object-oriented programming (OOP) that describe relationships between classes. Interviewers often ask this question to gauge a candidate's understanding of how objects interact within a system and to assess their grasp of design principles that promote code reusability and maintainability. Understanding these concepts is crucial for designing robust systems and avoiding tight coupling.
Composition implies a strong relationship between the whole and its parts, meaning that the lifetime of the contained objects depends on the lifetime of the container. For example, if a Car object is destroyed, all its Engine objects are also destroyed. In contrast, Aggregation represents a weaker relationship, where the contained objects can exist independently of the container. For instance, a Library can contain Books, but if the Library is deleted, the Books can still exist elsewhere.
Common misconceptions include confusing the two terms or assuming they are interchangeable. Understanding the differences helps in making better architectural decisions, ensuring proper ownership semantics, and enhancing code clarity.
Sample Answers
Example 1: Understanding Composition
In Composition, the relationship is characterized by a strong ownership. For instance, consider a House and Room classes. A House is composed of several Room objects, and if the House is destroyed, so are the Room objects. This relationship is often modeled in code as follows:
class Room {
// Room properties and methods
}
class House {
private List<Room> rooms;
public House() {
rooms = new ArrayList<>();
}
public void addRoom(Room room) {
rooms.add(room);
}
}
This example illustrates how the
Houseclass manages the lifecycle ofRoomobjects. The key takeaway is the notion of ownership and dependency in Composition, which ensures that the contained objects cannot exist without the parent object.
Example 2: Exploring Aggregation
In Aggregation, the relationship is much looser. For example, consider a Team and Player classes. A Team can have multiple Player objects, but if the Team is deleted, the Player objects can still exist independently. This can be represented in code as:
class Player {
// Player properties and methods
}
class Team {
private List<Player> players;
public Team() {
players = new ArrayList<>();
}
public void addPlayer(Player player) {
players.add(player);
}
}
In this case, the
Teamclass aggregatesPlayerobjects, highlighting their separate lifecycles. The distinction here is that while theTeammanages its players, it does not own them, allowing for greater flexibility and reusability of thePlayerobjects across different teams.
Example 3: Practical Implications in Design
Understanding the difference between Composition and Aggregation has significant implications in software design. For instance, if you are designing a system where certain components need to be tightly coupled, such as a Car and its Engine, Composition is the right choice. Conversely, if you are building a system where entities need to interact but retain their independence, such as Students and Courses, Aggregation is more appropriate.
For example:
- Composition: A
Bookand itsPages- if theBookis deleted, itsPagesare too. - Aggregation: A
Schooland itsStudents-Studentscan exist without theSchooland can be part of different schools over time.
Choosing the right relationship affects the maintainability and scalability of your code. This awareness can lead to better architectural decisions and ultimately more robust software solutions.
Keywords
Ready to practice more questions?
Explore our collection of technical interview questions from top companies.
View All Questions