Unit 3 Cycle 1 Day 14: Polymorphic References

Unit 3 Foundation (Cycle 1) Day 14 of 28 Foundation

Polymorphic References

Section 3.15 — Creating References

Key Concept

A polymorphic reference uses a superclass type to refer to a subclass object: Animal a = new Dog(). The compile-time type (Animal) determines which methods can be called, while the runtime type (Dog) determines which version of an overridden method executes. This means you can only call methods that exist in the declared type, but overridden methods use the actual object's version. The AP exam tests this dual-type system extensively — compile-time type for method availability, runtime type for method behavior.

Consider the following class hierarchy where Dog and Cat extend Animal.

public class Animal { public String sound() { return "..."; } } public class Dog extends Animal { public String sound() { return "Woof"; } } public class Cat extends Animal { public String sound() { return "Meow"; } }

What does the following code print?
Animal a = new Dog(); System.out.println(a.sound());

Answer: (B) "Woof"

The reference type is Animal, but the actual object is a Dog. At runtime, Java calls Dog's sound() method. This is polymorphism: the actual type determines method behavior.

Why Not the Others?

(A) The reference type is Animal, but the object is a Dog. The Dog version runs.

(C) The object is a Dog, not a Cat.

(D) A parent reference can hold a child object. Animal a = new Dog() is valid.

Common Mistake

Compile-time type (the declared type) determines what methods you CAN call. Runtime type (the actual object) determines which VERSION of the method runs.

AP Exam Tip

ParentType var = new ChildType(); is valid. The reference type limits which methods are visible, but the object type determines which overridden version executes.

Review this topic: Section 3.15 — Creating References • Unit 3 Study Guide

More Practice

Back to blog

Leave a comment

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