1Z0-819 · Question #29
Given: Automobile.java public abstract class Automobile { //line 1 abstract void wheels(); } Car.java public class Car extends Automobile { void wheels(int i) { // line 2 System.out.print(4); //…
The correct answer is A. Remove the parameter from wheels method in line 3. Option A is correct because Car.wheels(int i) overloads - not overrides - the abstract wheels() from Automobile. Since Car never actually implements wheels(), it is still effectively abstract and cannot be instantiated on line 4, causing a compile error. Removing the int i…
Question
Options
- ARemove the parameter from wheels method in line 3.
- BAdd @Override annotation in line 2.
- CChange the instantiation of line 4 to Car cb = new Car();
- DRemove abstract keyword in line 1.
How the community answered
(26 responses)- A81% (21)
- B4% (1)
- C4% (1)
- D12% (3)
Explanation
Option A is correct because Car.wheels(int i) overloads - not overrides - the abstract wheels() from Automobile. Since Car never actually implements wheels(), it is still effectively abstract and cannot be instantiated on line 4, causing a compile error. Removing the int i parameter makes the signature void wheels(), which properly overrides the abstract method, makes Car concrete, and allows cb.wheels() to print 4.
Why the distractors fail:
- B - Adding
@Overridetowheels(int i)would produce a compile error because there is no parent method with that signature to override;@Overridevalidates, it doesn't fix. - C - Changing the reference type to
Cardoesn't matter;Carstill fails to implement the abstract method, so it cannot be instantiated regardless of the declared type. - D - Removing
abstractfrom the class while leavingabstract void wheels()on the method just moves the problem -Automobileitself would now fail to compile as a non-abstract class containing an abstract method.
Memory tip: Think "override = exact match." Java requires the overriding method to have the identical parameter list; any difference in parameters creates a new overload instead, leaving the abstract contract unfulfilled.
Topics
Community Discussion
No community discussion yet for this question.