Method Overriding and Exception Hierarchy in Java : Free Java Tutorials

Method Overriding is as important as Method Overloading in Java. In this tutorial, we will understand Exception Hierarchy in Java and understand the rules that we follow when we override methods which throws different types of Exceptions.

If child class method throws any checked exception, then parent class method should compulsorily throw the same checked exception or its parent. Otherwise, we will get a compile-time error.

However, there are no restrictions for the unchecked exceptions.

In Java, the Throwable class is the root of the hierarchy of Exceptions.

Throwable class, RuntimeException and its child classes and Error and its child classes are unchecked exceptions.

All other Exception and Error classes represent checked exceptions/errors.

Java Exception Hierarchy

Method-Overriding-Exception-Hierarchy

Let’s take examples of various scenarios of child class and parent class method and understand rules related to Exception class hierarchy.

1) Parent class: public void method() throws Exception

Child class: public void method()

In this example, child class does not throw an exception. So, this is a valid combination of overriding.

2) Parent class: public void method()

Child class: public void method() throws Exception

In this example, child class throws Exception. But parent class does not throw any exception or its parent type exception. Thus, this is not a valid combination of overriding.

3) Parent class: public void method() throws Exception

Child class: public void method() throws IOException

In this example, child class throws IOException and parent class throws Exception which is a parent class of IOException. Hence, this is a valid combination of overriding.

4) Parent class: public void method() throws IOException

Child class: public void method() throws Exception

Here, child class throws Exception and parent class throws IOException which is not a parent class of Exception. Hence, this is not a valid combination of overriding.

5) Parent class: public void method() throws IOException

Child class: public void method() throws EOFException, FIleNotFoundException

In this example, child class throws EOFException as well as FileNotFoundException and parent class throws IOException which is parent class of EOFException and FileNotFoundException. Hence, this is a valid combination of overriding.

6) Parent class: public void method() throws IOException

Child class: public void method() throws EOFException, InterruptedException

In this example, child class throws InterruptedException and parent class does not throw InterruptedException or its parent type Exception. Therefore, this is not a valid combination of overriding.

Let’s implement this case 6 from above examples and see the compile-time error that we get.

Example of Method Overriding involving Exception Hierarchy

import java.io.*;

// Parent class
class Parent
{
	// parent class method which throws IOException
	public void method() throws IOException
	{
	}
}

//Child class
class Child extends Parent
{
	// child class method which throws EOFException and InterruptedException
	public void method() throws IOException,InterruptedException
	{
	}
}

We get a compile-time error saying Exception InterruptedException is not compatible with throws clause in Parent.method().

7) Parent class: public void method() throws IOException

Child class: public void method() throws ArithmeticException, NullPointerException

In this example, child class throws ArithmeticException as well as NullPointerException and parent class throws IOException which is parent class of both ArithmeticException and NullPointerException. This is a valid combination of overriding.

This is all about Exception Hierarchy and Method Overriding in Java. Hope you find the tutorial helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *