About Question enthuware.ocpjp.v7.2.1168 :
Moderator: admin
-
- Posts: 25
- Joined: Thu Jan 10, 2013 8:45 am
- Contact:
About Question enthuware.ocpjp.v7.2.1168 :
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?
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?
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
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?
throws Exception is neither compatible with throws IOException nor throws SQLException. So why should it be the right answer?
If you like our products and services, please help us by posting your review here.
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
Hi, I tried the code out and I added a little line to test some ambiguity:
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.
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");
}
}
Could you provide me with an example of interface method ambiguity? Thanks in advance.
The_Nick.
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
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.
Try with interface fields to see issues with ambiguity.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue Oct 28, 2014 2:11 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
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.
What do you mean in your reply by "not violate" and "compatible" in other words.
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
It does apply here. Why do you think it does not?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?
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.What do you mean in your reply by "not violate" and "compatible" in other words.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue Oct 28, 2014 2:11 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
I see, so valid answers are both:
and
Code: Select all
public void m1() throws FileNotFoundException {}
Code: Select all
public void m1() {}
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu Jul 31, 2014 8:46 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
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.
In this case m1 to throw no exception it's the only valid answer.
-
- Posts: 17
- Joined: Wed Jan 07, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
Just want to confirm
will be also valid right?
Code: Select all
public void m1() throws SQLException, IOException{}
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
What happened when you tried it out?
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Wed Jan 07, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
Just try it, hopefully my understanding is correct.
if I implements the m1() by following
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?
if I implements the m1() by following
Code: Select all
public void m1() throws SQLException, 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?
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1168 :
That is correct.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Google [Bot] and 9 guests