Multiple choice technology programming languages

package inheritance; class X { Y b = new Y(); X() { System.out.print("X"); } } class Y { Y() { System.out.print("Y"); } } public class Z extends X { Y y = new Y(); Z() { System.out.print("Z"); } public static void main(String[] args) { new Z(); } }

  1. YXYZ

  2. Z

  3. ZX

  4. ZXYY

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

When creating Z(), initialization order is: parent X's field b = new Y() runs first (prints 'Y'), then X() constructor executes (prints 'X'), then Z's field y = new Y() runs (prints 'Y'), finally Z() constructor executes (prints 'Z'). Output: YXYZ. This demonstrates that parent fields initialize before parent constructor, which runs before child fields/constructor.