Is-A and Has-A Relationship in Java

Introduction

In Java, we can reuse our code using an Is-A relationship or using a Has-A relationship. An Is-A relationship is also known as inheritance and a Has-A relationship is also known as composition in Java.

Is-A Relationship in Java

In Java, an Is-A relationship depends on inheritance. Further inheritance is of two types, class inheritance and interface inheritance. It is used for code reusability in Java. For example, a Potato is a vegetable, a Bus is a vehicle, a Bulb is an electronic device and so on. One of the properties of inheritance is that inheritance is unidirectional in nature. Like we can say that a house is a building. But not all buildings are houses. We can easily determine an Is-A relationship in Java. When there is an extends or implement keyword in the class declaration in Java, then the specific class is said to be following the Is-A relationship.

For example, the Animal class is a superclass of the Mammal class. This means that all mammals are animals, but not all animals are mammals.

The is-a relationship is implemented in Java using the extends keyword. The following code shows an example of the is-a relationship:

class Animal {
  public void eat() {
    System.out.println("Animal is eating");
  }
}

class Mammal extends Animal {
  public void giveBirth() {
    System.out.println("Mammal is giving birth");
  }
}

public class Main {
  public static void main(String[] args) {
    Mammal mammal = new Mammal();
    mammal.eat();
    mammal.giveBirth();
  }
}

In this code, the Mammal class extends the Animal class. This means that the Mammal class inherits all the properties and methods of the Animal class, including the eat() method. The Mammal class also has its own method, giveBirth().

When we create an instance of the Mammal class, we can call both the eat() and giveBirth() methods. This is because the Mammal class inherits the eat() method from the Animal class.

The is-a relationship is a powerful tool in object-oriented programming. It allows us to reuse code and create more complex and sophisticated programs.

Here are some of the benefits of using the is-a relationship in Java:

  • Code reuse: The is-a relationship allows us to reuse code by inheriting the properties and methods of a superclass. This can save us a lot of time and effort when writing code.
  • Reduced redundancy: The is-a relationship can help us to reduce redundancy in our code by avoiding the need to write the same code in multiple classes.
  • Increased flexibility: The is-a relationship can help us to make our code more flexible by allowing us to change the behavior of a subclass without affecting the superclass.
  • Enhanced readability: The is-a relationship can help to make our code more readable by making it clear how the different classes are related to each other.

Has-A Relationship in Java

In Java, a Has-A relationship is also known as composition. It is also used for code reusability in Java. In Java, a Has-A relationship simply means that an instance of one class has a reference to an instance of another class or another instance of the same class. For example, a car has an engine, a dog has a tail and so on. 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.

is-a and has-a relationship in java

Example

package relationsdemo;  
public class Bike  
{  
    private String color;  
    private int maxSpeed;  
    public void bikeInfo()  
    {  
        System.out.println("Bike Color= "+color + " Max Speed= " + maxSpeed);  
    }  
    public void setColor(String color)  
    {  
        this.color = color;  
    }  
    public void setMaxSpeed(int maxSpeed)  
    {  
        this.maxSpeed = maxSpeed;  
    }  
}

In the code above the Bike class has a few instance variables and methods.

package relationsdemo;  
public class Pulsar extends Bike  
{  
    public void PulsarStartDemo()  
    {  
        Engine PulsarEngine = new Engine();  
        PulsarEngine.stop();  
    }  
}

Pulsar is a type of bike that extends the Bike class that shows that Pulsar is a Bike. Pulsar also uses an Engine's method, stop, using composition. So it shows that a Pulsar has an Engine.

package relationsdemo;  
public class Engine  
{  
    public void start()  
    {  
        System.out.println("Started:");  
    }  
    public void stop()  
    {  
        System.out.println("Stopped:");  
    }  
} 

The Engine class has the two methods start( ) and stop( ) that are used by the Pulsar class.

package relationsdemo;  
public class Demo  
{  
    public static void main(String[] args)  
    {  
        Pulsar myPulsar = new Pulsar();  
        myPulsar.setColor("BLACK");  
        myPulsar.setMaxSpeed(136);  
        myPulsar.bikeInfo();  
        myPulsar.PulsarStartDemo();  
    }  
}

In the code above we make an object of the Pulsar class and then initialize it. All the methods like setColor( ), bikeInfo( ), setMaxSpeed( ) are used here because of the Is-A relationship of the Pulsar class with the Bike class.

Output

example of IS-A and HAS-A relationship

Summary

In this article, you learn about the Is-A and Has-A relationships in Java.


Similar Articles