• 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

Core java

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i have two interfaces in which method is same but diffirent exception.one class is implementing both the interfaces.do i need to implement both the method in that class?
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one is enough.Bacically what all it cares is about the signature of the method , not the exceptions that is throws.
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following scenario





In the above case Implementor will compile... But if it was throwing SQLException as the compiler (or IDE) will show an error stating that the exception thrown is not compatible with InterfaceB
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above case Implementor will compile... But if it was throwing SQLException as

code:

public int add(int x, int y) throws SQLException{

return x+y;
}

the compiler (or IDE) will show an error stating that the exception thrown is not compatible with InterfaceB



That's because while doing so you are not fulfilling one of the requirement for Overriding-

According to 'Java Language Specification',
  • A method declaration must not have a throws clause that conflicts with that of any method that it overrides. Otherwise, a compile-time error.
  • Overriding method must only throw an Exception that is more restrictive than overridden method.
  • Overriding method might decide not to thow any Exception.
  • Overriding method can throw RuntimeException & its subclasses.


  •  
    Ranch Hand
    Posts: 1274
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi cowboys,

    What Prathiba wrote is the cause why you cannot throw any exception here.
    Even this:

    will not compile.



    Yours,
    Bu.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic