What is Composition in Java With Examples

Java is a versatile language that supports object-oriented programming and code reusability with building relationships between two classes. There are two types of relationships or associations in Java used to reuse a code and reduce duplicity from one class to another.

These relationships are IS-A(Inheritance) and HAS-A (Association). While there is a tight coupling between the IS-A classes, HAS-A classes are loosely coupled and more preferable for the programmers.

The HAS-A relationship is divided into two types, viz., aggregation and composition in Java. This article is based on the OOP concept of composition. We will see many real-life examples of how the composition is coded and the advantages gained when implemented.

A Brief Narration of Associations or Relationships in Java

In object-oriented programming, objects are related to each other and use the common functionality between them. This is where the topics of Inheritance, Association, Aggregation, and Composition in Java programs come.

 Inheritance (IS-A) and Association (HAS-A) in Java

1. Inheritance (IS-A)

An IS-A relationship signifies that one object is a type of another. It is implemented using ‘extends’ and ‘implements’ keywords.

Example: HP IS-A laptop

Our learners also read: Learn java online free!

2. Association (HAS-A)

A HAS-A relationship signifies that a class has a relationship with another class. For instance, Class A holds Class B’s reference and can access all properties of class B.

Example: Human body HAS-A Heart




3. Aggregation Vs Composition

Has-A relationship or Association can be divided into aggregation and composition. An aggregation container class and referenced class can have an independent existence. A composition reference class cannot exist if the container class is destroyed.

Let’s take an example to understand aggregation and composition. A car has its parts e.g., engines, wheels, music player, etc. The car cannot function without an engine and wheels but can function without a music player. Here the engine and car have a composition relation, and the car and music player have an aggregation relationship. In the case of Aggregation, an object can exist without being part of the main object.



Composition in Java

A composition in Java between two objects associated with each other exists when there is a strong relationship between one class and another. Other classes cannot exist without the owner or parent class. For example, A ‘Human’ class is a composition of Heart and lungs. When the human object dies, nobody parts exist.

The composition is a restricted form of Aggregation. In Composition, one class includes another class and is dependent on it so that it cannot functionally exist without another class.

Implementation of Composition in Java

The engine and car relationship are implemented using Java classes as below. In Java, the ‘final’ keyword is used to represent Composition. This is because the ‘Owner’ object expects a part object to be available and function by making it ‘final’.

public class Car {

           private final Engine engine; 

    public Car(){

    engine  = new Engine();



 class Engine {

private String type;


Let us take another example that depicts both inheritance and composition.


In this program, the class Honda is a Car and extends from the class Car. The car engine Object is used in the Honda class.

class CarEngine {

    public void StartEngine(){

        System.out.println(“The car engine has Started.”);


    public void stopEngine(){

        System.out.println(“The car engine has Stopped.”);


class Car {

    private String colour;

    private int maxi_Speed;

    public void carDetails(){

        System.out.println(“Car Colour= “+colour + “; Maximum Speed= ” + maxi_Speed);


    //Setting colour of the car

    public void setColour(String colour) {

        this.colour = colour;


    //Setting maximum car Speed

    public void setMaxiSpeed(int maxi_Speed) {

        this.maxi_Speed = maxi_Speed;


class Honda extends Car{

    public void HondaStart(){

        CarEngine Honda_Engine = new CarEngine(); //composition



public class Main {

    public static void main(String[] args) {   

        Honda HondaJazz = new Honda();








Car Colour = Black; Maximum Speed = 160

The car engine has started. 

The output is derived using composition and shows the details of the Honda Jazz car.

UML Denotations of Association

The relationships of association, aggregation, and composition in Java between classes A and B are represented as follows in UML diagrams:

Association: A—->B

Composition: A—–<filled>B

Aggregation: A—–<>B

Get Software Engineering degrees online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Benefits of Composition in Java

Using composition design technique in Java offers the following benefits:

  1. It is always feasible to “prefer object composition over class inheritance”. Classes achieve polymorphism and code reuse by composition.
  2. The composition is flexible, where you can change class implementation at run-time by changing the included object, and change its behaviour.
  3. A composition-based design has a lesser number of classes.
  4. THE “HAS-A” relationship between classes is semantically correct than the “IS-A” relationship.
  5. Composition in Java offers better class testability that is especially useful in test-driven development.
  6. It is possible to achieve “multiple inheritances” in languages by composing multiple objects into one.
  7. In composition, there is no conflict between methods or property names.


Composition in Java offers many advantages while programming and is one of the favoured design methods. In this article, we have tried to make you understand this important concept with real-life examples and practical code. Composition offers flexibility and robust code. Its code reusability feature helps in avoiding code duplication and achieving cost-effectiveness. This makes it one of the widely used methods in various programs. 

Learn composition in Java with upGrad’s Master of Science in a Computer Science course which is aimed to make you learn and upgrade your skills in software development.


A Bachelor’s Degree with 50% or equivalent marks. No initial coding experience is needed.


The program fee starts at Rs.13, 095/month for Indian Residents and USD 5999 for International residents.

Want to share this article?

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks