Site icon Learn Automation

Is-A Relationship in Java

Has -A Relationship In Java

In this post, we will learn what is Is-A relationship in Java Oops concept and also we will see Association and its types.

Is-A used for code reusability in Java. An Is-A relationship is also known as Inheritance.

[Has-a and Is-a relationship in oops]

What is Is-A Relationship in Java?

Is-A relationship in java depends on inheritance. When a class or interface extends other classes or interfaces then it creates Is-A relationship in Java. It is used for code reusability and reduces the redundancy of code. It is used extends and implements keyword to creates Is-A relationship. In this relationship, one class acquired the all properties of another class.

In real world there are so many examples like potato is a vegetables, Honda is a bike, dog is a animal etc.


class Animal {

} class Dog extends Animal {


Note: In Is-A relationship, classes are tightly coupled means if we change the property of one class it will impact the property of other class.


Advantages of Is-A Relationship:

  1. Code reusability
  2. Cost cutting
  3. Reduce redundancy


Types of Relationship:-

In above image you can see types of relationship. There is two types of relationship.

Inheritance shows Is-A relationship and Association shows Has-A relationship.

We have already discussed Inheritance (Is-A relationship).

Now we will discussion about Association.

HAS-A Relationship in Java with example

Association in Java:

Association defines connection between two classes. It shows has-A relationship. It manages one-to-one, one-to-many, many-to-one and many-to-many relationships.

Example 1:

class Student{
         String name;
         int rollno;


Student has-A name

Student has-A rollno

This example shows the association or Has-A relationship.

Example 2:

class Engine
    class Car {
    Engine e = new Engine ();


Car Has-A Engine.

Note: 1. Here you can see we are not extending class Engine. But we are creating object of Engine class means using this object we can use properties of Engine class. Here, both classes are not tightly coupled.

2. If we will extend Engine class then both classes are tightly coupled and we can use all properties of Engine class in Car class. But in above example we can use only those properties which we needed and if we change some properties in Engine class, it will not impact to Car class.

Types of Association:

Aggregation: It defines HAS-A relationship in Java. It is also known as weak association because one entity can exist without other or if one entity fails due to some error, it will not impact to other one.

Example: A car has a music player. Here, car can exist without music player or vice versa. So here is weak bonding relationship.

Composition: It also defines HAS-A relationship in Java. It is also known as strong association because one entity cannot exist without others.

Example:  A car has a engine. Here, car cannot exist without engine or vice versa. So here is strong bonding relationship.

Example of both Aggregation and Composition:

In below image, we can see both weak and strong bonding relationship.

Between college and professor has a weak bonding because a college can exist without professor or vice versa. So it is called aggregation.

But college and branches has a strong bonding because branches cannot exist without college or vice versa. So it is called composition.


Has-A relationship in C++:

In C++, Aggregation is a process by which we can defines one class reference entity into another class. It is a form of association which show the Has-A relationship.

Read For more detail – Has-A relationship in C++


In this post, we have covered ‘Is-A Relationship in Java’. I have also covered What is Association and its type.

Final word, Bookmark this post  ‘Is-A Relationship in Java’ for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!


Must Read:

What is JDK, JVM and JRE?

What is Robot Class in Selenium?


Exit mobile version