Which code correctly maintains this meaning?

Given:
1. class X {
2. private Y y;
3. public X(Y y) { this.y = y; }
4. }
5. class Y {
6. private X x;
7. public Y() { }
8. public Y(X x) { this.x = x; }
9. }
The instance variable y is intended to represent the composition relationship “X is composed of Y.”
Which code correctly maintains this meaning?

Given:
1. class X {
2. private Y y;
3. public X(Y y) { this.y = y; }
4. }
5. class Y {
6. private X x;
7. public Y() { }
8. public Y(X x) { this.x = x; }
9. }
The instance variable y is intended to represent the composition relationship “X is composed of Y.”
Which code correctly maintains this meaning?

A.
X x1 = new X(new Y());
X x2 = new X(new Y());

B.
X xx = new X(null);
Y y1 = new Y(xx);
Y y2 = new Y(xx);

C.
Y yy = new Y();
X x1 = new X(yy);
X x2 = new X(yy);

D.
Y y1 = new Y(new X(null));
Y y2 = new Y(new X(null));



Leave a Reply 1

Your email address will not be published. Required fields are marked *


Diego da Silva

Diego da Silva

I did not get this answer. For me A y C are the same. Can someone explain me the difference?