Consider the following code:
public interface I1 { public void bar1() throws IOException; } public interface I2 extends I1 { public void bar2() throws Exception; } public interface I3 { public void bar3() throws Exception; } public abstract class A implements I2 { public void bar2() throws Exception{}; public void bar3() throws Exception{}; protected abstract void bar4(); protected void bar5() {}; }
Now I created the class Bas follows:
B
public class B extends A implements I3 { @Override protected void bar4() {} public void bar1() {} }
Why does the compiler allow me to do this? I mean, it should not be:public void bar1() throws IOException;
public void bar1() throws IOException;
When overriding, you cannot throw a wider or new exception. Superclass method exception exceptions are allowed.
, , , . , , FileNotFoundException, , SQLException, , FileNotFoundException.
.
, , , .
, super, .
, :
new B().bar1()
, , .
((A)new B()).bar1()
, A A, .
, C:
public class C extends A implements I3 { @Override protected void bar4() {} public void bar1() throws IOException, SomeOtherException {} }
, , C A, SomeOtherException.
Source: https://habr.com/ru/post/1547018/More articles:Fast graphics visibility solver? - algorithmLocationClient receive location updates by distance - androidWhy sometimes the Python subprocess failed to get the correct exit code after the process started? - pythonBuilding a sparse matrix from other smaller matrix values displayed by a logical array mask (MATLAB)? - matrixHow to set up communication through several models - ruby | fooobar.comJFreeChart Custom Color Chart? - javaRx JS Subscribe to an observer for several observations - javascriptCSS3 / JS gets element position during animation - javascriptNode.js reload dependencies without reloading the application - node.jsWhat is the default value of EditText padding? - androidAll Articles