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???
About Question enthuware.ocpjp.v8.2.1168 :
Moderator: admin
-
- Posts: 2
- Joined: Sat Jul 11, 2015 8:01 am
- Contact:
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1168 :
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.
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.
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.
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Jul 11, 2015 8:01 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1168 :
Paul, thank you for a nice and clear explanation.
I knew the concept, but for some reason it slipped my mind this time! Thanks!!!
I knew the concept, but for some reason it slipped my mind this time! Thanks!!!
-
- Posts: 6
- Joined: Sat Jun 03, 2017 1:43 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1168 :
Code: Select all
interface I1{ void m1() throws IOException;}
interface I2{ void m1() throws SQLException;}
public class Test implements I1, I2 {
void m1() {}
}
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.Cannot reduce the visibility of the inherited method from I2
-
- Posts: 8
- Joined: Thu Dec 07, 2017 4:43 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1168 :
I tried the proposed solution ( implementing method m() without throwing exception), but it doesn't work exception need to be thrown !!!!!!
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1168 :
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.
Who is online
Users browsing this forum: Bing [Bot] and 45 guests