Has-A Relationship in Java | Types, Example

Has-A Relationship in Java | In addition to Uses-A relationship and Is-A relationship that represents inheritance, one more relationship is commonly used in object-oriented programming. This relationship is called Has-relationship in Java.

When an object of one class is created as a data member inside another class, it is called Has-A relationship.

In other words, a relationship in which an object of one class has a reference to an object of another class or another instance of the same class is called Has-A relationship in Java.

Let’s understand Has-A relationship with the help of different kinds of examples.

Has-A Relationship Examples


1. A most common example of Has-A relationship in Java is “A person has an address”.

Has-A relationship denotes a whole-part relationship where a part cannot exist without the whole. In the above example, the person represents the whole and the address represents the part. A part cannot exist by itself.

In Java, there is no special feature by which we can identify the Has-A relationship in the code.

Person class has an instance variable of type Address as shown in the below code. An instance of Address class is created outside of Person class.

public class Address {
  // Code goes here.
}
public class Person {
// Person has-a Address.
   Address addr = new Address();

  // Other codes go here.
}


Look at the above figure where an object of class Address is created as a data member inside another class Person. This relationship is known as Has-A relationship.

Has-A Relationship in Java

In Java, there is no such keyword that implements a Has-A relationship. But we mostly use new keywords to implement a Has-A relationship in Java.

Let’s take another common example to understand Java Has-A relationship.

2. We know that a CPU is part of the computer. We can also rephrase this relationship as “A computer has a CPU”.

Does the existence of CPU outside a computer make any sense?

The answer is no. The existence of a CPU makes sense only within the computer. So, a computer and CPU represent the whole-part relationship.


Look at the below code to understand better.

public class CPU {
   // Code goes here.
}
public class Computer {
 // CPU part-of Computer.
    private CPU cpu = new CPU();

    // Other codes go here.
}

3. We know that a brain has a thought. Thought cannot exist without the existence of the brain. The code is given below:

public class Thought {
  // Code goes here.
}
public class Brain {
  Thought thought = new Thought();

// Other codes go here.
}

4. We know about a special class known as inner class in Java that can also be used to represent Has-A relationship. An instance of inner class can exist only inside an instance of its outer class (enclosing class). The outer class would be the whole and the inner class would be the part.

How to decide which type of Relationship we need?


We know that relationships between objects make all the differences in object-oriented programming. The most important relationships are Is-A relationship and Has-A relationship.

The best way to decide which kind of relationship we need is as follows:

a. If your problem with a phrase contains ” . . . . is a . . . .”. Then you should use Is-A relationship (Inheritance). For example, “A dog is a pet”. We cannot say “A dog has a pet”, as makes no sense at all.

So, in this case, we will create a superclass named Pet and a derived subclass named Dog.

b. On the other hand, if your problem with a phrase like this: “A pet has a name” then use has-a relationship. This is because if we use Is-A relationship instead of Has-A relationship, then the statement will be like this: “A dog is a name”. This statement makes no sense at all.

From all the above examples, you will have understood which kind of relationship you need in your code.

Types of Has-A Relationship in Java


There are two types of Has-A relationship that are as follows:

a. Aggregation
b. Composition

Aggregation: Aggregation is one of the core concepts of object-oriented programming. It focuses on establishing a Has-A relationship between two classes.

In other words, two aggregated objects have their own life cycle but one of the objects has an owner of Has-A relationship and child object cannot belong to another parent object.

For example, a library has students. If the library is destroyed, students will exist without library.

Composition: Composition is another one of the core concepts of object-oriented programming. It focuses on establishing a strong Has-A relationship between the two classes.

In other words, two composited objects cannot have their own life cycle. That is, a composite object cannot exist on its own. If one composite object is destroyed, all its parts are also be deleted.

For example, a house can have multiple rooms. A room cannot exist independently and any room cannot belong to two different houses. If the house is destroyed, all its rooms will be automatically destroyed.

Hope that this tutorial has covered all the important points related to Has-A relationship in Java with realtime examples. I hope that you will have understood this simple topic.

Please, share on social networking sites for your friends.
Thanks for reading!!!