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.
Class Car HAS-A engine reference.
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.
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.
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.
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.
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.
Here is the implementation of the same which is as follows:
Car class has a couple of instance variable and few methods
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.
The Engine class has the two methods start() and stop() that are used by the Maserati class.