ProgrammingVery hardProduct-company placement · Java

What happens when this is compiled?

What happens when this is compiled?
interface I { int f(); }
abstract class A { abstract long f(); }
class C extends A implements I {
    public long f() { return 1; }
}
  1. A. It compiles; C.f() implements both
  2. B. It compiles; calls through I return 1 cast to int
  3. C. Compile-time error only in class A
  4. D. Compile-time error: f() cannot satisfy both int f() and long f()

Correct answer: D. Compile-time error: f() cannot satisfy both int f() and long f()

C inherits two abstract methods with the same signature but incompatible return types (int and long). No single method can override both, so the class cannot be compiled.

More OOP with C++ and Java questions

Exams that ask this topic