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.

car class

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

That’s all about HAS-A relationship in Java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!

HAS-A Relationship
Scroll to top