Has-A Relationship

Back to home
Logicmojo - Updated Jan 11, 2023



What is Has-A Relationship?

HAS-A relationship is also known as composition (or) aggregation.There is no specific keyword to implement hAS-A relationship but mostly we can use new operator.The main advantage of HAS-A relationship is reusability.

In simple terms, it indicates that an instance of one class has a reference to another class's instance or another instance of the same class. This relationship aids in the reduction of code duplication as well as problems.

Example

Class Car HAS-A engine reference.

class Engine 
{
	//engine specific functionality
}
class Car
{
	Engine e=new Engine();
	//........................;
	//........................;
	//........................;
}

HAS-A relationship increases dependency between the components and creates maintains problems.

The relationship between two distinct classes that is established by their Objects is known as association. The two types of association are composition and aggregation. A Has-A connection is also known as composition in Java. It's also used in Java to make code more reusable. A Has-A connection in Java basically means that an instance of one class has a reference to another class's occasion or another occurrence of a comparable class. A vehicle, for example, has a motor, while a dog has a tail. There is no watchword in Java that executes a Has-A relationship. In Java, however, we usually use new catchalls to implement a Has-A relationship.

Composition vs Aggregation

Composition

Without existing container object if there is no chance of existing contained objects then the relationship between container object and contained object is called composition which is a strong association.

Example

University consists of several departments whenever university object destroys automatically all the department objects will be destroyed that is without existing university object there is no chance of existing dependent object hence these are strongly associated and this relationship is called composition.

Aggregation

Without existing container object if there is a chance of existing contained objects such type of relationship is called aggregation. In aggregation objects have weak association.

Example

Within a department there may be a chance of several professors will work whenever we are closing department still there may be a chance of existing professor object without existing department object the relationship between department and professor is called aggregation where the objects having weak association.

Implementation in Java

public class Car {

	private String color;
	private int maxSpeed;

	public static void main(String[] args)
	{
		Car nano = new Car();

		nano.setColor("RED");
		nano.setMaxSpeed(329);
		nano.carInfo();
		Maserati quattroporte = new Maserati();
		quattroporte.MaseratiStartDemo();
	}

	public void setMaxSpeed(int maxSpeed)
	{
		this.maxSpeed = maxSpeed;
	}

	public void setColor(String color)
	{
		this.color = color;
	}

	public void carInfo()
	{
		System.out.println("Car Color= " + color
						+ " Max Speed= " + maxSpeed);
	}
}

class Maserati extends Car {

	public void MaseratiStartDemo()
	{
		Engine MaseratiEngine = new Engine();
		MaseratiEngine.start();
		MaseratiEngine.stop();
	}
}

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

	public void stop()
	{
		System.out.println("Stopped:");
	}
}

Here is the implementation of the same which is as follows:

  1. Car class has a couple of instance variable and few methods

  2. Maserati is a type of car that extends the Car class that shows Maserati is a Car. Maserati also uses an Engine’s method, stop, using composition. So it shows that a Maserati has an Engine.

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


With this article at Logicmojo, you must have the complete idea of Has-A Relationship in OOps.