HAS-A Relation (Association)

Author: Ravi Poswal

Association is divided into two following parts:

1. Aggregation

2. Composition

1. Aggregation represents the whole relation between classes aggregation is a weaker relation between classes. i.e., it can be an independent existence class and container.

Example: - The room has chairs.

The room has fans.

2. Composition represents the mode up of relation between two or more classes. It is stronger relation in the way that the existence of both classes depends on each other for Example: -

Room has walls

The room has a roof.

FAQs

class A{
int a;
public A(int x){
a=x;
}
public void display(){
System.out.println(“a=”+ a);
}
class B{ A a;
int b;
public B(Ax, int y){
a=x;
b=y;
}
public void display(){
a.display();
System.out.println(“b=”+b);
}}
class Has-a-test{
public static void main(String a[]){
A x= new A(5);
B y= new B(x,10);
y.display();
}
}
Note: - Aggregation is implemented by containing an object of a class as a data member in another class.