The question asks "What line causes a compile time error?",
and the code compiles.
I have contacted the author regarding this question and have not received a response back.
I am posting the question in its entirety along with the code.
Author states the answer is C and since the code compiles answer is D.
Start of Question:
_________________________________________________
Given the following code :
Consider that Exception is the super class of IOException and IOException is the super class of FileNotFoundException.
What line causes a compile time error?
A. Line 3.
B. Line 6.
C. Line 17.
D. Compilation succeeds.
Answer : C
When overriding a method, it is legal to throw any runtime exception in the overriding method even if the method in the base class does not throw one.
It is also legal to throw a subclass of the checked exception that exists in the parent method. It is legal for the overriding method in the subclass not to throw any exception.
During compile time, the reference type decides which method should be invoked.
According to above explanation, we should try to catch a IOException or there will be Exception at line 6 because we used the interface 'Printable' as the reference type.
So at line 17, trying to catch FileNotFoundException causes a compile time error.
Therefore option C is correct.
______________________________________________________________________________________________________________________________________
End of Question:
-----
The code compiles and the correct option is D.
This is a mistake by the author.
Sir Thomas in my opinion when your class is implementing an interface and overriding a function, and use and you call that function always always and always the overridden version will be called which are throwing subtype exception (in this case FileNotFoundException) so we'll have to be ready for handling that type of Exception.
Yes the author made a mistake. Generally when subclassing you are allowed to make it easier for clients to use your new version but not harder (otherwise Liskov substitution fails). The Printer class can even remove the throws declaration altogether making it even easier for the Test class call that found method because then it won't be required to have a try/catch.