About Question enthuware.ocpjp.v7.2.1346:

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
enpayne
Posts: 3
Joined: Thu Oct 17, 2013 8:09 am
Contact:

About Question enthuware.ocpjp.v7.2.1346:

Post 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 ;)

enpayne
Posts: 3
Joined: Thu Oct 17, 2013 8:09 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

enpayne
Posts: 3
Joined: Thu Oct 17, 2013 8:09 am
Contact:

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

Post by enpayne »

Still, appreciate the super-fast answer Paul! :)

tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

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

Post by tn1408 »

I picked #5, None of the above, since the method: void m1(){} works. Not sure if I missed something here.

Thanks

Tony,

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

dk35840
Posts: 4
Joined: Wed Jun 05, 2019 6:44 am
Contact:

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

Post by dk35840 »

But how if m1() try to throw IOException.

By how it can be possible.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 37 guests