nerdexam
Oracle

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…

Java Object-Oriented Approach

Question

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); // line 3 } } public static void main(String[] args) { Automobile cb = new Car(); // line 4 cb.wheels(); } What must you do so that the code prints 4?

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)
  • A
    81% (21)
  • B
    4% (1)
  • C
    4% (1)
  • D
    12% (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 @Override to wheels(int i) would produce a compile error because there is no parent method with that signature to override; @Override validates, it doesn't fix.
  • C - Changing the reference type to Car doesn't matter; Car still fails to implement the abstract method, so it cannot be instantiated regardless of the declared type.
  • D - Removing abstract from the class while leaving abstract void wheels() on the method just moves the problem - Automobile itself 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

#Method overriding#Method overloading#Abstract methods#Polymorphism

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice