Page 1 of 1
About Question enthuware.ocpjp.v7.2.1168 :
Posted: Fri Apr 05, 2013 5:15 am
by Ambiorix
I don't follow the given explanation. It states, "the overriding declaration must have a throws clause that is compatible with ALL the overridden declarations".
By the overriding declaration, I assume that means the m1() method implemented in TestClass. In that case, shouldn't the correct answer be: public void m1() throws Exception?
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Fri Apr 05, 2013 6:39 am
by admin
Compatible means the throws clause of the overriding method must not violate the throws clause of the overridden method.
throws Exception is neither compatible with throws IOException nor throws SQLException. So why should it be the right answer?
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Tue Jun 11, 2013 3:26 am
by The_Nick
Hi, I tried the code out and I added a little line to test some ambiguity:
Code: Select all
public static void main(String args[]) throws Exception
{
IntefacesTricks tc = new IntefacesTricks();
I1 i1 = (I1) tc; //This is valid.
i1.m1();
tc.m1(); // added line
I2 i2 = (I2) tc; //This is valid too.
i2.m1();
}
public void m1()
{
System.out.println("Hi there");
}
}
The output is 3 times "Hi there". Should not be given a runtimeexception due to ambiguity? How does tc,m1() (without having been cast to any of the interfaces created) know which one to choose? It's acceptable that 2 different interfaces refer to the same method? Cool I did not know that..
Could you provide me with an example of interface method ambiguity? Thanks in advance.
The_Nick.
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Tue Jun 11, 2013 7:35 am
by admin
Interface methods never cause ambiguity issue because interface itself has no implementation. So no matter how many interfaces you implement with the same method, there can be only one implementation available in the target class. So the compiler invokes that method.
Try with interface fields to see issues with ambiguity.
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Tue Oct 28, 2014 2:15 pm
by jordanglassman
I think the question the OP meant to ask is why does the rule about overriding methods having equal to or narrower exceptions not apply here?
What do you mean in your reply by "not violate" and "compatible" in other words.
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Tue Oct 28, 2014 8:28 pm
by admin
jordanglassman wrote:I think the question the OP meant to ask is why does the rule about overriding methods having equal to or narrower exceptions not apply here?
It does apply here. Why do you think it does not?
What do you mean in your reply by "not violate" and "compatible" in other words.
If you throw a wider exception from an overriding method then that violates the throws clause of the overridden method. Not compatible is just another way of saying the same thing.
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Tue Oct 28, 2014 9:56 pm
by jordanglassman
I see, so valid answers are both:
Code: Select all
public void m1() throws FileNotFoundException {}
and
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Tue Oct 28, 2014 10:10 pm
by admin
Why do you think public void m1() throws FileNotFoundException {} is valid? As the explanation says, it should be compatible with the declarations from both the interfaces? Do you think it satisfies both of them?
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Tue Nov 04, 2014 7:59 am
by gablegca
Overriding methods can declare to throws the same exception, narrower exceptions, none or new runtime exceptions.
In this case m1 to throw no exception it's the only valid answer.
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Fri Apr 15, 2016 4:23 pm
by zhengye1
Just want to confirm
Code: Select all
public void m1() throws SQLException, IOException{}
will be also valid right?
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Fri Apr 15, 2016 8:10 pm
by admin
What happened when you tried it out?
-Paul.
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Sun Apr 17, 2016 6:45 pm
by zhengye1
Just try it, hopefully my understanding is correct.
if I implements the m1() by following
Code: Select all
public void m1() throws SQLException, IOException{}
the compiler will complain because he is trying to match the list of exception of base class(es), but actually m1() in interface I1 doesn't have SQLException, and m1() in interface I2 doesn't have IOException.
So in this case, the best way just not throws any exception in subclass, when the exception is actually occurred in the child class, let the parent to handle it, is that correct?
Re: About Question enthuware.ocpjp.v7.2.1168 :
Posted: Sun Apr 17, 2016 8:03 pm
by admin
That is correct.