Aggregation in Java
Nilu Kumari
Software Developer | Java | SQL | Data Structure & Algorithms | OOPS | JavaScript | HTML | CSS | Advance Java | Collection framework
Association:- Association is to show the relationship between two or more class. It shows Has-a-relationship.
We have tow association concept to show the Has-a- relationship
1. Aggregation
2. Composition
Here I will discuss Aggregation
Aggregation: Aggregation represent has a relationship, It show a weak relationship, means one class is weakly depend on another class.
Ex:- Suppose we have a car class and music class, so without music class car class can execute.
class Music{
private String songName;
Music(String songName){
this.songName=songName;
}
public String getsongName() {
return songName;
}
}
class Car{
private String Name;
private Music m;
领英推è
Car(String Name, Music m){
this.m=m;
}
public String getName() {
return Name;
}
public Music getMusic() {
return m;
}
}
public class Agreegation {
public static void main(String[] args) {
Music m1=new Music("Aashain hai");
Car c1=new Car("RR",m1);
System.out.println(c1.getName()+" music is "+c1.getMusic().getsongName());
}
}