ProgrammingHardIBM · Deloitte pattern

Does this compile?

Does this compile?
interface I { void m() throws java.io.IOException; }
class C implements I {
    public void m() throws InterruptedException {}
}
  1. A. Yes, any checked exception may be substituted when implementing an interface method
  2. B. No: InterruptedException is not a subtype of IOException, so the implementing method's throws clause is incompatible
  3. C. Yes, because interface methods never constrain throws clauses
  4. D. No, because implementing classes cannot declare checked exceptions at all

Correct answer: B. No: InterruptedException is not a subtype of IOException, so the implementing method's throws clause is incompatible

An implementing (or overriding) method's checked exceptions must be the declared type or a subtype of it. InterruptedException and IOException are unrelated checked exceptions, so C.m() violates the interface contract and fails to compile.

More OOP with C++ and Java questions

Exams that ask this topic