ProgrammingVery hardProduct company OA

What is printed?

What is printed?
class Vehicle {
    void start() { System.out.print("Vehicle starts "); }
}
interface Electric {
    default void start() { System.out.print("Electric starts "); }
}
class Tesla extends Vehicle implements Electric {
    @Override
    void start() { super.start(); Electric.super.start(); }
}
  1. A. Compilation error: start() is ambiguous between the class and the interface
  2. B. Compilation error: cannot call Electric.super.start() from a subclass
  3. C. Compilation error: default methods cannot be overridden
  4. D. Compiles fine: a class method always wins over an interface default without needing disambiguation, but explicit qualification is still allowed for clarity here

Correct answer: D. Compiles fine: a class method always wins over an interface default without needing disambiguation, but explicit qualification is still allowed for clarity here

Java's conflict-resolution rule states a concrete class method (even an inherited one from a superclass) always takes priority over an interface's default method, so there's no ambiguity to begin with — Tesla could even omit the override entirely and still get Vehicle::start. The explicit super.start() and Electric.super.start() calls shown here are both legal, deliberate ways to invoke each one specifically.

More OOP with C++ and Java questions

Exams that ask this topic