HAS-A Relationship

HAS-A Relationship

In this tutorial, we will discuss HAS-A relationship (also known as Composition or Aggregation) in Java. In Java, Composition (HAS-A) means that an instance of one class has a reference to an instance of another class. It is also used for code reusability in Java.There is no specific keyword, but most of the cases we can implemented by using new keyword.

HAS-A Relationship
public class Engine {
   m1(){}
   m2(){}
    .
    .
    .
}

public class Car {
   Engine e = new Engine();
  .
  .
}

Here Class car has an engine.

The main disadvantage of composition relationship is it increases dependency between components and creates maintainability problems.

HAS-A Relationship

Scroll to top