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
- Class Fruit has a data-member naming fruitName
- Class Mango extends the Class Fruit, which means that Mango is a type of Fruit.
-
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!”