Post

Java - Has-A and Uses-A Relationship

Java - Has-A and Uses-A Relationship

[toc]


In Java, reuse code using an Is-A relationship or Has-A relationship.

  • Is-A relationship is also known as inheritance
  • Has-A relationship is also known as composition in Java.
  • both used for code reusability in Java.

Is-A Relationship in Java

  • In Java, Is-A relationship depends on inheritance.
    • Further inheritance is of two types, class inheritance and interface inheritance.
  • inheritance is unidirectional in nature.
    • a Potato is a vegetable, a Bus is a vehicle.
    • a house is a building.
    • But not all buildings are houses.
  • determine an Is-A relationship in Java.
    • extends or implement keyword in the class declaration in Java
    • then the specific class is said to be following the Is-A relationship.

Has-A Relationship in Java

  • In Java, Has-A relationship is also known as composition.
  • Has-A relationship: an instance of one class has a reference to an instance of another class or an other 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

IS-A and HAS-A relationship

code example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
    }
}

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


// ====================================================================


public class Pulsar extends Bike {
// Pulsar is a type of bike that extends the Bike class that shows that Pulsar is a Bike.
// All the methods like setColor( ), bikeInfo( ), setMaxSpeed( ) are used because of the Is-A relationship of the Pulsar class with the Bike class.

    public void PulsarStartDemo() {
        Engine PulsarEngine = new Engine();
        // Pulsar also uses an Engine's method, stop, using composition.
        PulsarEngine.stop();
    }
}


// ====================================================================

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


This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.