How to understand two classes mutual "has a" relationship?

S

Shawn

Hi,

I saw a UML diagram showing two classes A and B. A "has a" B in it. B
"has a" A in it. I feel hard to understand.

Could you help me or give me some code to show that concept?

Thank you very much.
 
C

Chris

Shawn said:
Hi,

I saw a UML diagram showing two classes A and B. A "has a" B in it. B
"has a" A in it. I feel hard to understand.

Could you help me or give me some code to show that concept?

Thank you very much.

class A {
private B b;

public B getB() {
return b;
}
}

class B {
private A a;

public A getA() {
return a;
}
}

Pretty simple.
 
E

Eric Sosman

Shawn said:
Hi,

I saw a UML diagram showing two classes A and B. A "has a" B in it. B
"has a" A in it. I feel hard to understand.

Could you help me or give me some code to show that concept?

The thing to remember is that "has a" really means
"has a reference to a."

You give your friend a piece of paper with your telephone
number, and she gives you a piece of paper with hers. Neither
of you has swallowed the other, and there's no crazy infinite
recursion of mutual swallowing: Each of you walks away with
piece of paper, and each has the ability to call the other.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top