Page 1 of 1

About Question enthuware.ocpjp.v7.2.1346:

Posted: Thu Oct 17, 2013 8:23 am
by enpayne
I'm having trouble understanding this. If a class says it implements these two interfaces, the correct way of doing it is as follows:

Code: Select all

interface I1 {
	void m1() throws IOException;
}

interface I2 {
	void m1() throws FileNotFoundException;
}

class IDontGetIt implements I1, I2 {
    @Override
    public void m1() throws FileNotFoundException {	
    }
}
Why is this invalid?

Code: Select all

class ThisIsHowIThinkItShouldBe implements I1, I2 {
    @Override
    public void m1() throws IOException {	
    }
}
My thoughts are as follows:

A class implementing these two interfaces, if it throws any exceptions, must satisfy both exceptions thrown in the interfaces. It would make more sense to me if it was valid to throw an IOException, since FileNotFoundException IS-A IOException, but IOException is not necessarily a FileNotFoundException (upcast, downcast). So declaring "throws IOException" would satisfy both interfaces. However throwing FileNotFoundException does not always satisfy I1, since the IOException could, for example, be a EOFException. With the above implementation it is not possible to have an implementation that throws EOFException, although interface I1 would allow it. I know I'm missing a point, I just can't see it ;)

Re: About Question enthuware.ocpjp.v7.2.1346:

Posted: Thu Oct 17, 2013 8:32 am
by enpayne
Well there you go, writing it all down and thinking about it again, I think I've come up with the answer myself. Allowing the implementing class to throw any IOException would not satisfy I2, so the more specific Exception must be thrown in order to satisfy both interfaces. Right?

Re: About Question enthuware.ocpjp.v7.2.1346:

Posted: Thu Oct 17, 2013 8:55 am
by admin
What you need to think here is that a class that implements just the interface I1, is free to throw any subclass of IOException not just FNE. So if you try to say that this class also implements I2 (because both have the same method m1), it will break contract presented by I2, which says that m1 can throw only FNE.

For example, lets say you have a class C1:

Code: Select all

class C1 implements I1{
  public void m1() throws  IOException { 
     throw new EOFException(); //This is valid because EOFE is a subclass of IOException.
  }
}
But if you change the above code to:

Code: Select all

class C1 implements I1, I2{
  public void m1() throws  IOException{  //This is NOT valid anymore because I2's m1 says it can throws only FNE.
     throw new EOFException();
   }
}
Therefore, if to satisfy both the interfaces, you need:

Code: Select all


class C1 implements I1, I2{
  public void m1() throws  FileNotFoundException{  //This is valid for I1 as well as I2 because  FNE is a subclass of IOException

   //of course, you cannot now throw new EOFException here 
  }
}

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1346:

Posted: Thu Oct 17, 2013 8:57 am
by admin
enpayne wrote:Well there you go, writing it all down and thinking about it again, I think I've come up with the answer myself. Allowing the implementing class to throw any IOException would not satisfy I2, so the more specific Exception must be thrown in order to satisfy both interfaces. Right?
Saw your post after I posted the above. Yes, you got it :)
-Paul.

Re: About Question enthuware.ocpjp.v7.2.1346:

Posted: Thu Oct 17, 2013 8:59 am
by enpayne
Still, appreciate the super-fast answer Paul! :)

Re: About Question enthuware.ocpjp.v7.2.1346:

Posted: Sat Mar 22, 2014 7:08 pm
by tn1408
I picked #5, None of the above, since the method: void m1(){} works. Not sure if I missed something here.

Thanks

Tony,

Re: About Question enthuware.ocpjp.v7.2.1346:

Posted: Sat Mar 22, 2014 8:58 pm
by admin
Yes, void m1(){ } works. But that doesn't make "None of the above" right because one of the above options is indeed right.

Re: About Question enthuware.ocpjp.v7.2.1346:

Posted: Fri Jul 19, 2019 5:53 am
by dk35840
But how if m1() try to throw IOException.

By how it can be possible.

Re: About Question enthuware.ocpjp.v7.2.1346:

Posted: Fri Jul 19, 2019 9:21 am
by admin
dk35840 wrote:
Fri Jul 19, 2019 5:53 am
But how if m1() try to throw IOException.

By how it can be possible.
Can you please explain what you mean?