Unit 3 Cycle 2 Day 27: Comprehensive Inheritance Trace
Share
Comprehensive Inheritance Trace
Section Mixed — Review: All Unit 3
Key Concept
Comprehensive inheritance tracing problems combine constructor chaining, method overriding, polymorphic dispatch, and field access into a single multi-step trace. These represent the most challenging Unit 3 questions on the AP exam. The key is to separate the trace into phases: first trace constructor execution (top-down), then trace each subsequent method call (checking runtime type for dispatch). Write down the complete object state after construction before tracing any method calls.
Consider the following classes.
What does new Bar().process(4) return?
Answer: (B) 24
Bar inherits process() from Foo. process(4) calls calc(4). The object is a Bar, so Bar's calc runs: 4*3=12. process returns 12*2=24.
Why Not the Others?
(A) 10 = (4+1)*2, using Foo's calc. But the object is Bar, so Bar's calc is used.
(C) 14 does not match any calculation path.
(D) 5 = 4+1, using Foo's calc without the *2.
Common Mistake
Inherited methods that call overridden methods use the override. Foo.process() calls calc(), but since the object is Bar, Bar.calc() runs. This is dynamic dispatch.
AP Exam Tip
This pattern (parent method calling overridden method) appears on nearly every AP exam. The actual object type always determines which override runs.