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.
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 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.
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
Summary
In this article, you learn about the Is-A and Has-A relationships in Java.