We can override a method by changing only the exception handling in java.
An overriding method (the method of child class) can throw any unchecked exceptions, regardless of whether the overridden method (method of base class) throws exceptions or not.
However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw those checked exceptions, which have less scope than the exception(s) declared in the overridden method.
package com.tutorial4u;
class A {
void m1() {
System.out.println("method A");
}
}
class B extends A {
void m1() throws NullPointerException {
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
Output : Method B
Case 2 : If superclass doesn’t throw any exception but child class throws an checked exception
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() {
System.out.println("method A");
}
}
class B extends A {
// Exception IOException is not compatible with throws clause in A.m1()
void m1() throws IOException {
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
Output : Exception in thread "main" java.lang.Error: Unresolved compilation problem: Exception IOException is not compatible with throws clause in A.m1()
Note : We cannot throw a checked exception if the overridden method(method of base class) is not throwing an exception.
Case 3 : If superclass throws checked exception but subclass throws narrower (subclass of) checked exception
package com.tutorial4u;
import java.io.FileNotFoundException;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
void m1() throws FileNotFoundException {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) throws IOException {
A a = new B();
a.m1();
}
}
Output : method B
Case 4 : If superclass throws checked exception but subclass throws broader (superclass of ) checked exception.
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
// Exception
Exception is not compatible with throws clause in A.m1()
void m1() throws Exception {
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
try {
a.m1();
} catch (Exception
e) {
e.printStackTrace();
}
}
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Exception Exception is not compatible with throws clause in A.m1()
Note : Getting compilation error because the m1() method of child class is throwing Exception which has a broader scope than the exception thrown by method m1() of parent class.
But if child class method have child exception of the super class declared exception or same or not throwing any exception. It will execute fine.
Case 5 : If superclass throws checked exception but subclass throws an unchecked Exception (RuntimeException) package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
// Exception Exception is not compatible with throws clause in A.m1()
void m1() throws ArithmeticException {
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
try {
a.m1();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output : Method B
Case 6 : If superclass throws unchecked Exception but subclass throw a checked exception.
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws ArithmeticException {
System.out.println("method A");
}
}
class B extends A {
// Exception
IOException is not compatible with throws clause in A.m1()
void m1() throws IOException{
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
try {
a.m1();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Exception IOException is not compatible with throws clause in A.m1()
Case 7: If superclass and subclass both throw an unchecked Exception (RuntimeException)
package com.tutorial4u;
class A {
void m1() throws ArithmeticException {
System.out.println("method A");
}
}
class B extends A {
void m1() throws NullPointerException
{
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
Output : method B
Case 8 : If superclass and subclass both throws a checked exception
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
void m1() throws IOException {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) throws IOException {
A a = new B();
a.m1();
}
}
Output : method B
Case 9 : if superclass throws a checked exception but subclass does not throw any exception.
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
void m1() {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) throws IOException {
A a = new B();
a.m1();
}
}
Output : method B
Case 10 : If superclass throws an unchecked exception but subclass doesn't throw any exception
package com.tutorial4u;
class A {
void m1() throws ArithmeticException {
System.out.println("method A");
}
}
class B extends A {
void m1() {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
Output : method B