Unit 3 Cycle 2 Day 23: Constructor Chaining with super

Unit 3 Advanced (Cycle 2) Day 23 of 28 Advanced

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.

public class A { private int x; public A(int val) { x = val; } public int getX() { return x; } } public class B extends A { private int y; public B(int a, int b) { super(a); y = b; } public int sum() { return getX() + y; } } public class C extends B { public C(int val) { super(val, val * 2); } }

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.

Review this topic: Section 3.14 — The super Keyword • Unit 3 Study Guide

More Practice

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.