1Z0-809 · Question #71
Given: Which modification enables the code fragment to print Happy Journey!?
The correct answer is B. Replace line n1 with protected void ride() throws Exception {. Option B works because it declares the parent class's ride() method as protected and declares throws Exception - the broadest checked exception type. This allows the child class's override (at n2) to have equal-or-wider access (public ≥ protected) and throw a narrower checked…
Question
Options
- AReplace line n1 with public void ride() throws FuelNotAvailException {
- BReplace line n1 with protected void ride() throws Exception {
- CReplace line n2 with void ride() throws Exception {
- DReplace line n2 with private void ride() throws FuelNotAvailException {
How the community answered
(25 responses)- A4% (1)
- B84% (21)
- C8% (2)
- D4% (1)
Explanation
Option B works because it declares the parent class's ride() method as protected and declares throws Exception - the broadest checked exception type. This allows the child class's override (at n2) to have equal-or-wider access (public ≥ protected) and throw a narrower checked exception (FuelNotAvailException extends Exception), satisfying both of Java's method overriding rules.
Why A fails: Swapping n1 to throw FuelNotAvailException (a narrow exception) means the child's override cannot throw the broader Exception - overriding methods can only throw the same or narrower checked exceptions, never broader ones.
Why C fails: Changing n2 to package-private (no modifier) is more restrictive than a public or protected parent method - overriding methods must maintain or widen access, never restrict it.
Why D fails: private methods are not inherited and cannot be overridden. The subclass would silently create a brand-new method rather than an override, so polymorphic dispatch would never call it - "Happy Journey!" would never print.
Memory tip: Think "open wider, throw narrower" - when overriding, you can only open the access door (same or wider) and shrink the exception list (same or narrower checked exceptions).
Community Discussion
No community discussion yet for this question.