• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

similar named methods in interface

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried a simple test case of a class that implements 2 interfaces that have similar method signatures but different exceptions that are thrown.

I seem to have a problem compiling it.

However if I take out the exceptions that are thrown it seems to work. Can someone explain this.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AFAIK the methods in the interfaces, if they have the same signatures, must be declared as throwing the same exceptions. And the class implementing the overridden method must declare that the method throws those exceptions (or a subclass of them).
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,
Can you confirm if it is mandatory for an overridden method in a subclass also to declare the exception thrown by the parent class?? Even though using a code sample this does not seems to be mandatory but I am remember reading about it somewhere? or a similar kind of requirement...



compiles fine
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a subclass of a class you may declare that your overridden method throws the same exception as the superclass method, one or more subclasses of the exception, or no exception at all. You may not throw a checked exception that the superclass does not throw. It is generally a good idea to declare the throws even if you are not going to throw and exception, otherwise you restrict subclasses of your class in their ability to throw exceptions.

If you are implementing two interfaces with the same method signature other than the exceptions, you will be restricted to throwing only exceptions that both interfaces throw.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still confused if you have 2 interface that have similar method signatures but throw different exceptions how do I handle it if I implement both.

I tried to throw IOException on one and IllegalArgument on another.

For some reason I only can throw IllegalArgument but if I put both on the overriden method I get compiler error.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I previously said, you will have to put both throws clauses into both interfaces' method declarations.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post you code here?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the signatures the same or are the similar.

If they are the same you cannot throw all the Exceptions that both interfaces throw.

You can only throw Exceptions that would be legal from both. I think an example would best explain.



Think about it this way.

If you create an instance of class C as such

A a = new C();

When you call

a.someMethod()

it would be illegal for it to throw a SAXException while when

B b = new C();
b.someMethod()

it would be illegal for it to throw a SQLException.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your example cleared it up. I thought its possible to throw both distinct exemptions once you implement both of them. I tried it but coul'dnt get it to compile but I understand the confusion the compiler is having. Thanks!!!
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic