Full Explained Aggregation, Composition, And Association In Java.

0
115
Association, Aggregation, and Composition in ava

In this article, we will discuss about Aggregation, Composition, and Association in Java. also, learn some of the basic concepts like types of association. Has a Relationship and Is a Relationship in java? Let’s Get Started from the beginning.

What is Association in Java?

Association is one more concept of Object Orientation in java. It represents the relationship between one or multiple Objects. And also it refers to how objects get associated with another Object. as well as how they are using functionality with each other.

Some Association Examples in Java:-

According to the definition of association, it represents the relationship between two objects. you can observe in the picture how these objects are associated with each other.

Example:

  • Pen object with a notebook object.
  • A fish object with a water object.
  • A mobile object with chargers object.

Observe around yourself, their are so many objects.

"Association Examples in Java"

Must Read:- Grow you Java Programming Knowledge with our java quiz Section.

How Many Types of Association in Java?

In the common terms, There are 2 forms of Association.

  1. Composition
  2. Aggregation.

What is Composition in Java?

A composition is a special form of association. where an Object can not present logically without the existence of the Owner Object. If the Owner Object is going to be destroyed.

then associated object is also automatically destroyed. Hence it’s called a Strong Relationship in java.

Here is Some Composition Example in Java:-

We are considered as an example like:- Human and heart.

Every Human has a heart. In this Picture, Human object contains the heart and heart can’t exist without Human. is called Strong dependency between two objects.

"Composition Example in Java"
//Every Human must have a Heart
public class Human {
   //make private because its a core part of the human body
   private final Heart heart; //final will make sure Heart is initialized
   public Human () {
      heart = new Heart();
   }
}
//Heart Object
class Heart {
    private String type;
}

Must Watch:- Checkout the Complete List of Core java Topics.

What is Aggregation in Java?

Aggregation is also a special form of association. where an associated Object can be present logically or physically without the existence of the Owner Object. If the Owner Object is destroyed and still an associated Object can exist.

Hence it is called Week Relationship in java.

Here is Some Aggregation Example in Java:-

We are considered as an example like:- Car and Driver.

In this Picture, we have A car object and a Driver object. if Car is going to be destroyed, it will not affect the driver object. and the driver object can exist without a Car.

Hence is called week dependency between two objects.

"Aggregation Example in Java"
public class Car {  //Car Class
   private Driver;
}

class Person {   //Person class whose have a different identity
    String name;
}

Difference Between Aggregation Vs Composition?

Here is the some major difference between aggregation vs composition in Java:

No.AggregationComposition
1Aggregation is a special form of Association.Composition is a restricted form of Aggregation.
2It can be present physically without the Owner Object.It can not present logically and physically without the Owner Object
3Hence aggregation have a week dependency Relationship.Hence Composition have a Strong dependency.
4It represents “Has-A Relationship“.It represents “Is-A Relationship“.
5In this Case objects can be survive without owner object.In this case objects can not survive without each others.
6We can achieve Code reuse with aggregation.The composition also allows the reuse of code.

How Many Types of Relationships in Java?

In Java, There are 2 Types of Relationships.

  1. Has-A Relationship
  2. Is-A Relationship

What is “Has A Relationship in Java”?

Usually, the has-a relationship represents the object’s existence in java. Let’s suppose we have two objects A and object B. If we destroy object A and it does not affect object B, that means object B can live without Object A, which is Called Has-A relationship in java.

In other words, any object which can be live without the owner object, is considered as a Has a relationship between these objects.

What is “Is A Relationship in Java”?

In the java, Is a relationship is also showing the logical existence of objects. if we destroy object A then object B will Automatically destroy. because according to the composition definition. any object which is strongly connected with each other, like “human and heart ” is Called an Is-A relationship in java.

UML Diagram of “Association, Aggregation, and Composition”.

In java UML denotes different notations for association, aggregation, and composition.

  • Association denoted with a normal arrow.
  • aggregation demoted by red filed diamond-head arrow
  • and Composition denoted with the black filed diamond-head arrow.

When you draw a UML diagram for 2 related Classes, for example- Class Human and Heart. where Human class is associated with the Heart class. then according to the UML diagram, it’s denoted by Human->Heart.

Using UML Similarly we can show aggregation and composition between 2 related classes.

Here is the diagram for UML notations with the help of diagram you can get an idea what kind of dependency between two classes.

"UML notations in java"

Just observe the diagram, where composition representing the strongest form of relationships and association shows general forms of relationships.

"criteria of relationship in java"

Conclusion:-  Now it is enough for you at the starting point of java basics. So mostly we cover all topics related to the Association, aggregation, and Composition, as well as UML (Unified Modeling Language).

It’s all-important topics before going to the next lesson. you need to know what kind of relationship exists in java. There are only two ways to represent the relationship between multiple objects which are composition and aggregation.

Admin Words:- In this article, we cover all basic information about the association, aggregation, composition, has a relationship, Is a relationship, types of association with examples, as well as differences aggregation vs composition. I hope this will help you to make it easy to understand basic concepts.

If you have any questions regarding this article please feel free to ask in the comment box of this article.

0 0 vote
Article Rating
Next articleJVM Architecture | How JVM Works? | JVM in Java Full Explained
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments