Composition in Java

Last Updated : 7 Jan 2026

Composition and inheritance both are design techniques. The Composition is a way to design or implement the "has-a" relationship while inheritance is used to implement the "is-a" relationship. It is used to ensure the code reusability in our program. In Composition, we use an instance variable that refers to another object.

The composition relationship of two objects is possible when one object contains another object, and that object is fully dependent on it. The contained object should not exist without the existence of its parent object. In a simple way, we can say it is a technique through which we can describe the reference between two or more classes. And for that, we use the instance variable, which should be created before it is used.

It represents a strong association, if the parent object is destroyed, the contained object also ceases to exist.

Composition in Java

Important points to Remember

  • The Composition represents a part-of relationship.
  • Both entities are related to each other in the Composition.
  • The Composition between two entities is done when an object contains a composed object, and the composed object cannot exist without another entity. For example, if a university HAS-A college-lists, then a college is a whole, and college-lists are parts of that university.
  • Favor Composition over Inheritance.
  • If a university is deleted, then all corresponding colleges for that university should be deleted.

Let's take an example of a university and its colleges to understand the concept of Composition.

We create a class College that contains variables, i.e., name and address. We also create a class University that has a reference to refer to the list of colleges. A University can have more than one collages. So, if a university is permanently closed, then all colleges within that particular university will be closed because colleges cannot exist without a university. The relationship between the university and colleges is Composition.

Example

Compile and Run

Output:

Name: ABES Engineering College and Address: Ghaziabad
Name: AKG Engineering College and Address: Ghaziabad
Name: ACN College of Engineering & Management Studies and Address: Aligarh

Advantages of Composition

  • Composition allows us to reuse the code.
  • In Java, we can use multiple Inheritance by using the composition concept.
  • The Composition provides better test-ability of a class.
  • Composition allows us to easily replace the composed class implementation with a better and improved version.
  • Composition allows us to dynamically change our program's behavior by changing the member objects at run time.
  • It avoids diamond problem.

When to Use Composition?

We should use composition in the following cases:

  • If we want flexibility and avoid tight coupling
  • If multiple behaviors are needed without inheritance limitations.
  • If we are designing modular systems (for example, frameworks like Spring rely heavily on composition).
  • When lifecycle dependency is natural (for example, Room belongs to Building).

Composition Vs. Inheritance

AspectComposition (Has-A)Inheritance (Is-A)
DefinitionIt involves creating complex objects by combining simpler ones, composition in OOPs.It is a mechanism where a new class is derived from an existing class.
RelationshipIt establishes Has-a relationship (strong association) between objects.It establishes is-a relationship (hierarchical, parent-child) between objects.
FlexibilityHighly flexible because it can change behavior at runtime.It has low flexibility because tightly coupled to superclass.
DependencyObjects can exist independently of each other.Dependents on the base class, changes to which can affect derived classes.
ComplexityIt typically leads to simpler, more modular code.It may lead to complex class hierarchies that might be harder to understand.
EncapsulationIt pramotes encapsulation as objects hide their internal details.It may violate encapsulation as derived classes have access to all members of the base class.
Code ReuseAchieved via contained objects.Achieved via superclass methods.
Multiple BehaviorsPossible (because combines different classes).Not possible (because no multiple inheritance allowed).
ExampleCar has an Engine.Dog is an Animal.