• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface Method Signatures  RSS feed

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question may belong in "beginner"...

If a superclass implements some interface, can the derived class implement those interface but throw an Exception that the superclass interface does not? If not, why?

For example:

Thanks in advance for your feedback.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the codeNow, as far as the compiler is concerned, the "var" variable implements the contract defined by interface A. This contract doesn't include the possibility of throwing a checked exception. Therefore B cannot redefine that method to throw a checked exception, because that isn't part of the contract. Otherwise "methodA()" in the code example above could throw a checked exception which is not handled properly.

It's quite all right for the reverse to happen: an interface declares that a method throws some checked exception, and a class implementing that interface doesn't declare that. In this case there can be no rogue exceptions being thrown about.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Thanks for your reply. So let me now ask, as long as the check exception is properly handled there is no issue, correct? Is this approach a reflection of poor design?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shannon Sims:
Hi Paul,
Thanks for your reply. So let me now ask, as long as the check exception is properly handled there is no issue, correct? Is this approach a reflection of poor design?


To answer your original question, yes you can override and declare more exceptions - as long as those exceptions are unchecked (subclass of RuntimeException) or subtypes of the checked exceptions that are declared on the supertype.

The "poor design" issue is full of controversy - many people agree with the fact that exceptions are of a poor design, but often for the wrong reasons.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,
Thank you for your feedback.


To answer your original question, [BOLD]yes you can override and declare more exceptions - as long as those exceptions are unchecked [/BOLD](subclass of RuntimeException) or subtypes of the checked exceptions that are declared on the supertype.


What if the exceptions are checked exceptions? What are the implications of throwing a checked exception? Will the checked exception bubble up?

Thanks for your feedback!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to override with a method that throws checked exceptions which weren't declared as thrown in the original method declaration, the compiler won't let you. (have you tried it at all?) This suggests you will need to modify some part of the design, yes.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,
Thanks for your reply. No, I have not yet tried to run the application to test the object that throws the checked exception. Since I didn't get any compile errors, I thought maybe the overridden methods would work, but wanted to get some clarification before proceeding. Thanks for clarifying my confusion.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!