Unit 3 Cycle 2 Day 10: Abstract Thinking: What Should Be Overridden
Share
Abstract Thinking: What Should Be Overridden
Section 3.13 — Overriding Methods
Key Concept
Deciding which methods to override in a subclass requires understanding the superclass contract and the subclass specialization. A method should be overridden when the subclass needs different behavior than the default. Methods that should typically be overridden include toString() (for meaningful output), equals() (for value comparison), and any behavior method where the subclass acts differently. The AP exam tests this conceptually by asking which methods in a hierarchy should be overridden to achieve specific behavior.
Consider the following class hierarchy.
What does System.out.println(new Rectangle(3, 4)); print?
Answer: (A) Shape
Rectangle does NOT override toString(). It inherits Shape's version, which returns "Shape". The area() method is overridden but toString() is not.
Why Not the Others?
(B) println calls toString(), not area(). toString() returns "Shape".
(C) Shape overrides Object's toString(), so the default hash format is not used.
(D) toString() does not return the dimensions.
Common Mistake
Overriding one method does not affect others. Rectangle overrides area() but not toString(). Each method must be overridden independently.
AP Exam Tip
Check which methods are overridden in the subclass. If toString() is not overridden, the inherited version runs. Do not assume all methods are overridden.