aspose file tools*
The moose likes Beginning Java and the fly likes Core java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Core java" Watch "Core java" New topic
Author

Core java

sahidul karim
Greenhorn

Joined: Sep 19, 2006
Posts: 28
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?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Only one is enough.Bacically what all it cares is about the signature of the method , not the exceptions that is throws.


Rahul Bhattacharjee
LinkedIn - Blog
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
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


http://www.skillassert.com


Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199

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.



  • ~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
    Burkhard Hassel
    Ranch Hand

    Joined: Aug 25, 2006
    Posts: 1274
    Hi cowboys,

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

    will not compile.



    Yours,
    Bu.


    all events occur in real time
     
    Don't get me started about those stupid light bulbs.
     
    subject: Core java