Unit 3 Cycle 2 Day 23: Constructor Chaining with super
Share
Constructor Chaining with super
Section 3.14 — The super Keyword
Key Concept
Constructor chaining with super() across multiple inheritance levels executes from the top of the hierarchy down. Each subclass constructor calls its parent's constructor, which calls its parent's constructor, and so on until Object's constructor runs. The AP exam traces three-level constructor chains where each constructor prints something or initializes fields. The output order reveals the top-down execution: grandparent constructor first, then parent, then child.
Consider the following classes.
What does new C(5).sum() return?
Answer: (C) 15
C(5) calls B(5, 10). B(5,10) calls A(5), sets y=10. sum() returns getX()+y = 5+10 = 15.
Why Not the Others?
(A) 5 is just getX(). The sum includes y=10.
(B) 10 is just y. The sum includes getX()=5.
(D) The constructor chain is valid: C calls super(val, val*2) which matches B's constructor.
Common Mistake
Constructor chaining flows upward: C to B to A. Each level initializes its own fields. The sum() method accesses data from both B (y) and A (x via getX()).
AP Exam Tip
Multi-level constructor chaining is tested on the AP exam. Trace the super() calls from the bottom up to understand what each level initializes.