Unit 3 Cycle 1 Day 13: Overriding Methods
Share
Overriding Methods
Section 3.13 — Overriding Methods
Key Concept
Method overriding occurs when a subclass defines a method with the same signature (name, parameters, return type) as a method in the superclass. The overriding method replaces the superclass behavior for subclass objects. The override must have the same return type (or a subtype), the same parameters, and must not reduce accessibility (e.g., a public method cannot be overridden as private). On the AP exam, overriding is distinguished from overloading: overriding replaces behavior with the same signature, while overloading provides a different parameter list.
Consider the following classes.
What does new Square(5).describe() return?
Answer: (B) "Area: 25.0"
describe() is inherited from Shape and calls area(). Since the object is a Square, Java uses Square's overridden area() which returns 5*5=25.0. Result: "Area: 25.0".
Why Not the Others?
(A) Shape's area() returns 0, but it is overridden by Square's version.
(C) 5 is the side length, not the area.
(D) Square inherits describe() from Shape. No compilation error.
Common Mistake
This demonstrates polymorphism. Even though describe() is defined in Shape, when it calls area(), Java uses the overridden version from Square because the actual object is a Square.
AP Exam Tip
A parent method calling another method that is overridden in a child class will use the child's version. This is dynamic dispatch and is heavily tested on the AP exam.