This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface method throws exception

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Please verify.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you declared 'p' correctly. The explanation seems to suggest it should be a Printable, not a Printer.
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The following code causes a compiler error on line 17



I looked at the original source code from the online Test and they have listed:



in class Test.

So the author intended to use



but the code has listed




 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget to QuoteYourSources please.
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

When you say "Quote your Sources",
do you mean the source of the exam question?
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:do you mean the source of the exam question?

Reasons why are explained here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic