Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Composition and Aggregation in Java code

User_19BPUApr 25 2012 — edited Apr 25 2012
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2012
Added on Apr 25 2012
6 comments
890 views