Java OCA OCP Practice Question 652

Question

Which of these method signatures is allowed in a class implementing the Outfielder interface?


class Exception1 extends Exception {} 

class Exception2 extends Exception1 {} 

? 
public interface Outfielder { 
   public void catchBall() throws Exception2; 
} 
  • A. public int catchBall() throws Exception2
  • B. public int catchBall() throws Exception1
  • C. public int catchBall() throws Exception
  • D. None of the above


D.

Note

Trick question! Options A, B, and C are each invalid overrides of the method because the return type must be covariant with void.

For this reason, Option D is the correct answer.

If the return types were changed to be void, then Option A would be a valid override.

Options B and C would still be incorrect, since overridden methods cannot throw broader checked exceptions than the inherited method.




PreviousNext

Related