About Question enthuware.ocpjp.v8.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
Greg_Pro
Posts: 2
Joined: Sat Jul 11, 2015 8:01 am
Contact:

About Question enthuware.ocpjp.v8.2.1168 :

Post by Greg_Pro »

Consider these two interfaces:
interface I1 {    void m1() throws java.io.IOException; }
interface I2 {    void m1() throws java.sql.SQLException; }
What methods have to be implemented by a class that says it implements I1 and I2 ?

it is clear that if m1 declares none of the exceptions above than it is OK.
But, why provided option: public void m1() throws SQLException, IOException; is incorrect???

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

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

Post by admin »

A class that implements a method of an interface (or that overrides a method of a super class), cannot include any new exceptions in its throws class. It is only allowed throw either the same exceptions or any subclass of those exceptions as declared by the interface method. It is also allowed to not throw any exception at all.

Therefore, if you declare SQLException as well as IOException in your throws clause, neither of the interface methods will be satisfied.

Think about how it will work. A user of I1 interface expects only an IOException to be thrown and is prepared for that. If, at run time, the user gets an SQLException, his code will break because he is not prepared to handle SQLException.

Code: Select all

 I1 i1 = getI1(); //This method returns an instance of some class that implements I1.
 try{
    i1.m1(); 
 }
 catch(IOException ie){ //prepared to handle IOException but not SQLException
    //do something here.
 }
Same goes for the user of I2 interface. User of I2 interface knows only about the possibility of an SQLException to be thrown. He will be blindsided if the method throws IOException.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Greg_Pro
Posts: 2
Joined: Sat Jul 11, 2015 8:01 am
Contact:

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

Post by Greg_Pro »

Paul, thank you for a nice and clear explanation.
I knew the concept, but for some reason it slipped my mind this time! Thanks!!!

JavaSoftware
Posts: 6
Joined: Sat Jun 03, 2017 1:43 pm
Contact:

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

Post by JavaSoftware »

Code: Select all

interface I1{   void m1() throws IOException;}
interface I2{   void m1() throws SQLException;}

public class Test implements I1, I2 {
	void m1() {}
}
if anyone wonders why this code complains with
Cannot reduce the visibility of the inherited method from I2
it is because all interface methods are implicitly public and in this snippet the method visibility inside Test class is reduced to default which is less visible.

yassine
Posts: 8
Joined: Thu Dec 07, 2017 4:43 am
Contact:

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

Post by yassine »

I tried the proposed solution ( implementing method m() without throwing exception), but it doesn't work exception need to be thrown !!!!!!

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

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

Post by admin »

Please post the exact and complete code that you tried.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests