Page 1 of 1

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. publ

Posted: Sun Jun 11, 2023 4:01 pm
by answerhappygod
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));