FlibbyFlobby Posted October 29, 2004 Share Posted October 29, 2004 Yo, I need to aggregate one class into another using java, does anyone know the code to set this up? :blink: i know the code for subclassing is: class2 extends class1 but is there a way to define a class so it knows its an aggregated class? hope this made sense! :p thanks! Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted October 29, 2004 Share Posted October 29, 2004 Yo,I need to aggregate one class into another using java, does anyone know the code to set this up? :blink: i know the code for subclassing is: class2 extends class1 but is there a way to define a class so it knows its an aggregated class? hope this made sense! :p thanks! 584827530[/snapback] Aggregates are a "has a" relationship. If ClassA has a ClassB as a member variable, the ClassB is an aggregate of ClassA. Link to comment Share on other sites More sharing options...
0 Mouton Posted October 29, 2004 Share Posted October 29, 2004 but is there a way to define a class so it knows its an aggregated class?hope this made sense! :p Not really... What do you mean by 'aggregated' ? Use an example maybe to explain what you mean... Link to comment Share on other sites More sharing options...
0 FlibbyFlobby Posted October 29, 2004 Author Share Posted October 29, 2004 Not really...What do you mean by 'aggregated' ? Use an example maybe to explain what you mean... 584827649[/snapback] the best example i can give is that the class cat is an aggregation of head, torso and legs if you see what i mean, how would i implement this relationship in code? i stumbled across this just after i posted the thread: public class Money{} public class Wallet { private Money money; public Wallet(Money money) { this.money = money; } } can anyone verify this is how aggregation is setup? Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted October 29, 2004 Share Posted October 29, 2004 the best example i can give is that the class cat is an aggregation of head, torso and legs if you see what i mean, how would i implement this relationship in code?i stumbled across this just after i posted the thread: public class Money{} public class Wallet { private Money money; public Wallet(Money money) { this.money = money; } } can anyone verify this is how aggregation is setup? 584827661[/snapback] Head, torso and legs is a Composite, not an Aggregate. A composite has elements that cannot exist without the other. You can't have a body without a head, or the body dies... right? In other words, their lifetimes are dependent upon each other. Aggregates are independent of each other, and their lifetimes don't depend on one another. The Money/Wallet is an aggregate because both can be independent of each other. A wallet can have no money, and money can exist outside of a wallet. So, yes, that is how an aggregate is done. Link to comment Share on other sites More sharing options...
0 Mouton Posted October 29, 2004 Share Posted October 29, 2004 There's not much special about such... You define a class, and then create a variable of that type... much like creating a String variable (the String class is defined somewhere!) That you wrote or not the class you're using isn't that relevant, as long as the compiler can find it ("import xxx;" if in a diff. package / dir). BTW, you can't define 2 classes like that in a single .java file. If you want to have 2 classes insinde a file, one must be defined inside the other: public class Wallet { // Class definition public class Money{} // Member of Wallet, of type Money private Money money; [...] } Link to comment Share on other sites More sharing options...
0 FlibbyFlobby Posted October 29, 2004 Author Share Posted October 29, 2004 @weenur: your right my example was wrong, it is composite i was describing, my bad :p @mouton: it makes sense now! :D thanks for your help :) im building a system for a university assignment and my group has never touched on aggregation in java, only on UML diagrams :no: Link to comment Share on other sites More sharing options...
Question
FlibbyFlobby
Yo,
I need to aggregate one class into another using java, does anyone know the code to set this up? :blink:
i know the code for subclassing is:
but is there a way to define a class so it knows its an aggregated class?
hope this made sense! :p
thanks!
Link to comment
Share on other sites
6 answers to this question
Recommended Posts