Given the following example code, which overrides are valid and which one aren't (with explanation)?

private abstract static class C1 {
public abstract void m1() throws Exception;
public abstract void m2() throws IOException;
public abstract void m3() throws Exception;
private static class C2 extends C1 {
public void m1() throws IOException {}
public void m2() throws Exception {}
public void m3() {}
1 answers

Overrides of m1() and m3() are valid. In the case of m1(), the override in C2 defines an Exception which is a sub-class of the one declared on the parent. This is defining a more precise exception than the parent, and it's completely legal. In the second case, m2() throws a more general exception, and this is an illegal override.

shilpigupta22 shilpigupta22 [Author] 8 months ago votes 0