Java OCA OCP Practice Question 605

Question

Which of following method signatures would not be allowed in a class implementing the Printer interface?

class Exception1 extends Exception {} 

class Exception2 extends Exception1 {}? 

public interface Printer { 
       abstract int printData() throws Exception1; 
} 
  • A. public int printData() throws Exception2
  • B. public int printData() throws Exception
  • C. public int printData()
  • D. None of the above


B.

Note

Overridden methods cannot throw new or broader checked exceptions than the one they inherit.

Since Exception is a broader checked exception than Exception1, Option B is not allowed and is the correct choice.

Alternatively, declaring narrower or the same checked exceptions or removing them entirely is allowed, making Options A and C incorrect.

Option B is correct

Option D is incorrect.




PreviousNext

Related