• 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

Whats wrong about this code ?

 
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i am preparing for OCPJP exam (iz0-804) from Apress's "A comprehensive OCPJP7 certification guide". I found this statement in Chapter 11 (Exceptions and Assertions)

"If a method is declared in two or more interfaces, and if that method declares to throw different exceptions in the throws clause, the implementation should list all of these exceptions". I decided to practically check and see what happens



I replace method sigature line with options from 1 -5 and below is what happened
1, = gave no compilation error
2 = gave compile error saying "doNothing() is not compatible with the throws clause in A.doNothing()
3 = gave compile error saying "doNothing() is not compatible with the throws clause in B.doNothing()
4 = gave compile error saying "doNothing() is not compatible with the throws clause in A.doNothing()
5 = gave compile error saying "doNothing() is not compatible with the throws clause in B.doNothing()

What wrong here ? Is the statement in the book wrong or my understanding is wrong ? How can one remove compilation error if he/she face situation like i placed above ...
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having Generic Exception at interface level and Specific one at implementation level solves the issue. But I am not sure whether you are looking at such a solution.

This compiles fine:



I am using Java 7.
 
Bartender
Posts: 3922
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some points:
1) I would not expect such ambiguous questions on real exam - same method on several interfaces, and both implemented by same class
2) Regarding this piece: "If a method is declared in two or more interfaces, and if that method declares to throw different exceptions in the throws clause, the implementation should list all of these exceptions" I would say that it's wrong.
General rule which needs to be remembered: "Implementation MAY list [checked] exception declared in the interface, OR the exception's subclasses, but NOT their superclasses".

Quick example:



 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaxhif Khan wrote:"If a method is declared in two or more interfaces, and if that method declares to throw different exceptions in the throws clause, the implementation should list all of these exceptions". I decided to practically check and see what happens


Really like your attitude! Writing little code snippets to see what's happening, is always a good thing to do as it's very instructive.

Kaxhif Khan wrote:What wrong here ? Is the statement in the book wrong or my understanding is wrong ? How can one remove compilation error if he/she face situation like i placed above ...


The statement in the book is completely wrong!

Only if both method declarations in the interfaces define unchecked (runtime) exceptions, the statement in the book is true. Example:But as you know, with runtime exceptions you don't have to list the exceptions in the method declaration, so this will compile as well:

If both method declarations in the interfaces define a checked exception from a different class hierarchy (like in your original post), your code will only compile if the implementing method defines no checked exceptions in the throws clause (as you discovered yourself):Every other combination of checked exception(s) will NOT compile (and that's why the statement in the book is completely wrong). As you know the throws clause of the implementing method can list as many runtime exceptions as you want:

If both method declarations in the interfaces define a checked exception from the same class hierarchy (like in your original post), your code will only compile if the implementing method defines no checked exceptions in the throws clause or the most specific checked exception (or one of its subclasses). A few examples to illustrate:Class A56A compiles because no checked exceptions are listed in the throws clause. Class A56B compiles as well, because IOException IS-A Exception (and the most specific exception). Class A56C compiles too, because FileNotFoundException IS-A IOException (and a subclass of the most specific exception). And finally class A56D will not compile, because Exception isn't the most specific exception.

What if the method declaration in the first interface defines a checked exception and the method declaration in the second interface defines a runtime exception? Your code will only compile if the implementing method defines no checked exceptions in the throws clause.

That was easy, wasn't it?

Hope it helps!
Kind regards,
Roel
 
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

Girish Bal wrote:Having Generic Exception at interface level and Specific one at implementation level solves the issue. But I am not sure whether you are looking at such a solution.


Your code snippet is totally different than the code in the original post as your code list exactly the same method declaration in both interfaces. So you can't compare them at all.
 
Girish Bal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Girish Bal wrote:Having Generic Exception at interface level and Specific one at implementation level solves the issue. But I am not sure whether you are looking at such a solution.


Your code snippet is totally different than the code in the original post as your code list exactly the same method declaration in both interfaces. So you can't compare them at all.



It was the change I made to the original code to get it work. That's why it is "totally different" ...
 
Kaxhif Khan
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for quick and informative feedback, i tried earlier to post reply but i always got "aah you got us with our pants down ==> Busy Server" ...!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic