Table of contents
1.
Introduction
2.
Is-A-relationship
3.
How to achieve is-a relationship
4.
Implementation
5.
FAQs
5.1.
Can we implement is-a relationship in Java using the interface?
5.2.
Is is-a relationship unidirectional or bidirectional?
5.3.
Which keyword is used to implement is-a relationship with classes?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Is-A Relationship in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is an object-oriented programming language that supports inheritance. Inheritance is the ability to inherit the features and functionalities from another class. It supports the reuse of the same code. It allows the programmer to define a class in terms of another class. A relationship in Java means different relations between two or more classes. For example, if a class named Mango inherits another class named Fruit, then we can say that Mango is having is-a-relationship with Fruit, which implies Mango is a Fruit.

Also see, Duck Number in Java

Is-A-relationship

When one class inherits another class, it is called is-a-relationship in Java. It is completely related to inheritance. For example, Mango is a fruit.

  • Using the extends keyword, we can implement is-a relationship.
  • We use is-a relationship to avoid code redundancy and promote code reusability.
  • Is-a relationship is true only in one direction, that is, it's unidirectional. For example, when we say Mango is a fruit, it's vice-versa; Fruit is a mango is not true as there are other fruits too.
  • It is tightly coupled, that is, changing one entity will affect the other entity.

How to achieve is-a relationship

Is-a relationship can be achieved in two ways, either by extending an interface or by extending a class using the extend keyword.
 


The above flowchart represents that the class Mango extends the class Fruit, which means that the class Fruit is the parent class or the base class of the class Mango, and the class Mango is said to have an is-a relationship. Hence, we can say Mango is-a Fruit.

Syntax

Class base{
}
Class derived extends base{
}

Implementation

  1. Class Fruit has a data-member naming fruitName
  2. Class Mango extends the Class Fruit, which means that Mango is a type of Fruit.
  3. Since the Fruit Class is the parent class of the Mango Class, the Fruit Class can store the reference of an instance of the class Mango.
     
import java.io.*;
  
// Base class
class Fruit {
  
    private String fruitName;
    
    // constructor
    public void setFruitName(String fruitName)
    {
        this.fruitName = fruitName;
    }
    // print function
    public String geFruitName()
    {
        return this.fruitName + " is a Fruit";
    }
}
// Derived class
class Mango extends Fruit {
    public static void main(String gg[])
    {
        // base class stores the reference
        // of instance of derived classes
        Fruit fruit = new Mango();
    
        System.out.println("Fruit name is Mango");
        fruit.setFruitName("Mango");

        System.out.println(fruit.geFruitName());
    }
}


Output

Fruit name is Mango
Mango is a Fruit


Try it on Online Java Compiler.

 

Also read Has-A Relationship in Java here.

Must Read Type Conversion in Java

FAQs

Can we implement is-a relationship in Java using the interface?

Yes, we can implement is-a relationship in Java using the interface.

Is is-a relationship unidirectional or bidirectional?

Is-a relationship is unidirectional, for example, iPhone is-a mobile, is true, but the vice-versa of this, mobile is-a iPhone, is not true as there are other mobiles too.

Which keyword is used to implement is-a relationship with classes?

We use the extends keyword to implement is-a relationship with classes.

Conclusion

In this article, we have extensively discussed what is-a relationship is and how we can implement it in Java programming language with examples and their implementation in Visual Studio Code.

Check out this article - Java Tokens. Read more, Java Verify
We hope that this blog has helped you enhance your knowledge regarding is-a relationship in the Java programming language and if you would like to learn more, check out our articles on what is Multiple Inheritance in Java, what is Hybrid Inheritance in Java, what is hierarchical inheritance in Java.

Do upvote our blog to help other ninjas grow. Happy Coding!”

Live masterclass