• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface method throws exception

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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




 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget to QuoteYourSources please.
 
Thomas Hauck
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

When you say "Quote your Sources",
do you mean the source of the exam question?
 
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thomas Hauck wrote:do you mean the source of the exam question?


Reasons why are explained here.
 
reply
    Bookmark Topic Watch Topic
  • New Topic