aspose file tools
The moose likes Beginning Java and the fly likes Interface implementation Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Interface implementation Exception" Watch "Interface implementation Exception" New topic
Author

Interface implementation Exception

karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
Hi ranchers,

Why we cannot use new checked exceptions for interface's implementation methods in the sub class ,but there is no restriction with respect to run time exceptions ?

can you please elaborate on this .


karim
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32611
    
    4
Because an implementation is a refinement of the interface specification; whenever the specification is feasible, the refinement must be feasible. If you throw an Exception, the refinement is no longer feasible, so you are breaching the laws of refinement (and probably the Liskov Substitution Principle, too).

The compiler doesn't check RuntimeExceptions. It doesn't mean you can use RuntimeExceptions, it means the compiler doesn't throw an error. That is different.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I simpler explanation: added a (wider) checked exception changes the method signature while adding a RuntimeException doesn't.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
Wouter Oet wrote:I simpler explanation: added a (wider) checked exception changes the method signature while adding a RuntimeException doesn't.

Well, there are several things you can do when overriding or implementing a method that change the signature, which are perfectly legal. You can make a method more public, or you can make the throws clause more restrictive (throwing fewer possible exception types than the original did), or you can make the return type a subtype of the original return type. Changing the signature is allowed - but only changes in specific ways.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32611
    
    4
Mike Simmons wrote: . . . can make a method more public, or you can make the throws clause more restrictive . . .
Both those things increase the range of feasibility of the method, so they are consistent with refinement.

I think we have all described the same thing in three different ways.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

But you can only make changes that compatible with the old one.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
Right - I'm just saying that Wouter's simplified explanation is, in my opinion, too simplified, and misleading. We can't decide whether this code will compile using a "does it change the signature?" rule - we have to understand the rules for which changes are compatible. I'm sure the three of us all understand this fine - I'm just talking about how to present it to others.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
Although technically, none of these things (exceptions, return type, or access specifier) are part of the signature as defined in the Java Language Specification. So perhaps it's best to agree that no, we can't change the signature when overriding or implementing. But aside from that, we need to understand the rules about what changes are allowed for exceptions, return type, or access specifier.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32611
    
    4
Agree with MS: if we change the signature, the method is not overridden but overloaded.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface implementation Exception
 
Similar Threads
Need help on DAO
writing from a jar
Interface and exceptions
question on exception(HF P559)
Why we need custom exceptions?