Composition and Aggregation in Java code
Hi,
Regarding Composition and Aggregation , let us take the below example:-
public class Composite implements WhatIThink{
private Member member=new Member();
private AnotherMember anotherMember =new AnoterMember();
public Result doSomething(){
int a= member.whatHeSays();
return anotherMember.doSomethingWithResult(a);
}
}
public class Aggregate implements WhatIThink{
private Member state;
public Aggregate(Member state){
this.state=state;
}
public Result doSomething(){
return state.doSomething();
}
}
public interface WhatIThink{
//This is wat I think abt aggregation/Commposition
//both are some form of association
}
Here for Composition if we delete the Composite class the member object as well as the AnotherMember which is created as a new object will be deleted as well.
Right?
Where in the case or Aggregation we are using the reference of the member Class, hence whether it don't we deleted when the Whole (Aggregate) class is deleted? As this reference will be set in a memory and can be reused? Please clarify how these works from the java coding point of view.
Thanks.