• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interface method implementation

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from enthuware:


Consider these two interfaces:


interface I1
{
void m1() throws IOException;
}
interface I2
{
void m1() throws FileNotFoundException;
}


What methods have to be implemented by a class that says it implements I1 and I2 ?

The answer given is
public void m1() throws FileNotFoundException {}

I thought the answer would be public void m1() throws IOException {} ,since the implementing method should be able to handle both the exceptions .Did I miss something here?
ok obviously I did but cannot figure it out...
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You must remember:
Implementing class must not declare any new or broader checked exceptions for declared interface method. It is possible to throw narrower or uncheck exceptions.
(BTW: constructor of subclass must declare all the checked exceptions declared in the base constructor or may add other exception. This is exactly opposite.)

Because FileNotFoundException is subclass of IOException, FileNotFoundException is ok for both method.
 
Zdenek Pine
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p.s. and it is also possible to throw no exception.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at it this way: Your implementing class IS-AN I2, in which the method is declared to throw a FileNotFoundException. So to fulfill the contract with I2, the following must work:

If your method implementation throws an IOException, this will not work.

On the other hand, if your method implementation throws a FileNotFoundException, then your class satisfies both contracts, because it can be treated as either an I1 or an I2.
 
sumi rankan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to above posts..

If you use a reference of I1 to refer the implementation object then when you call the method m1() you should either catch IOException or declared to be thrown by that method since compiler checks the reference I1.
[ October 21, 2008: Message edited by: Vijitha Kumara ]
reply
    Bookmark Topic Watch Topic
  • New Topic