About Question enthuware.ocpjp.v7.2.1168 :

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

Moderator: admin

Post Reply
Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

About Question enthuware.ocpjp.v7.2.1168 :

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

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

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

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

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

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

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

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

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

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

jordanglassman
Posts: 4
Joined: Tue Oct 28, 2014 2:11 pm
Contact:

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

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

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

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

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

jordanglassman
Posts: 4
Joined: Tue Oct 28, 2014 2:11 pm
Contact:

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

Post by jordanglassman »

I see, so valid answers are both:

Code: Select all

public void m1() throws FileNotFoundException {}
and

Code: Select all

public void m1() {}

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

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

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

gablegca
Posts: 1
Joined: Thu Jul 31, 2014 8:46 am
Contact:

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

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

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

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

Post by zhengye1 »

Just want to confirm

Code: Select all

public void m1() throws SQLException, IOException{}
will be also valid right?

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

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

Post by admin »

What happened when you tried it out?
-Paul.
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

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

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

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

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

Post by admin »

That is correct.
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 9 guests