• 0

[JAVA]Aggregation Relationship Help!


Question

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

6 answers to this question

Recommended Posts

  • 0
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

  • 0
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

  • 0
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

  • 0
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

  • 0

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

  • 0

@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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.