ProgrammingVery hardProduct-company placement · Java

What happens?

What happens?
interface Show { String toString(); }
class C implements Show { }
// in main:
System.out.print(new C() instanceof Show);
  1. A. Compile-time error: C must implement toString()
  2. B. It compiles but calling toString() throws AbstractMethodError
  3. C. It compiles and prints true: Object's public toString() implements Show.toString()
  4. D. Compile-time error: an interface cannot redeclare toString()

Correct answer: C. It compiles and prints true: Object's public toString() implements Show.toString()

An interface may redeclare a public Object method as abstract. Every class inherits a public toString() from Object, which satisfies it, so C is concrete.

More OOP with C++ and Java questions

Exams that ask this topic