ProgrammingHardProduct-company placement · Java

What happens when Z is compiled?

What happens when Z is compiled?
interface X { int V = 1; }
interface Y { int V = 2; }
class Z implements X, Y {
    int get() { return V; }
}
  1. A. get() returns 1
  2. B. get() returns 2
  3. C. Compile-time error: a class cannot implement two interfaces with the same field
  4. D. Compile-time error: the reference to V is ambiguous

Correct answer: D. Compile-time error: the reference to V is ambiguous

Inheriting two constants with the same name is allowed, but a simple-name use of V is ambiguous; writing X.V or Y.V fixes it.

More OOP with C++ and Java questions

Exams that ask this topic