OOPS concepts like inheritance are an integral part of Java as it is an obejct oriented language. When a class extends a super class it can also override methods of the super class.
But what about the exceptions thrown by a super class method? Should the overridden method in the child class also need to throw the same exceptions or it can change it. To address these scenarios there are some rules laid out. In this post Exception Handling and Method Overriding in Java we'll talk about those restrictions.
Broadly there are two rules for exception handling with method overriding in Java-
- If superclass method has not declared any exception using throws clause then subclass overridden method can't declare any checked exception though it can declare unchecked exception with the throws clause.
- If superclass method has declared an exception using throws clause then subclass overridden method can do one of the three things.
- sub-class can declare the same exception as declared in the super-class method.
- subclass can declare the subtype exception of the exception declared in the superclass method. But subclass method can not declare any exception that is up in the hierarchy than the exception declared in the super class method.
- subclass method can choose not to declare any exception at all.
Examples of Exception handling and method overriding in Java
Let's see examples of the scenarios listed above to make it clear.
If superclass method has not declared any exception
If superclass method has not declared any exception using throws clause then subclass overridden method can't declare any checked exception though it can declare unchecked exception.
It can be noted here that parent class' displayMsg() method deosn't have any throws clause whereas overridden method in the subclass declares IOException in its throws clause which is a checked exception. That's why the compile time error.
If we change the throws clause in subclass method to any unchecked exception then it won't result in compiler error.
public void displayMsg() throws ArrayIndexOutOfBoundsException{ }
If superclass method has declared an exception
- If superclass method has declared an exception then sub class can declare the same exception as
declared in the superclass method.
class Parent{ public void displayMsg() throws IOException{ System.out.println("In Parent displayMsg()"); } } public class ExceptionOverrideDemo extends Parent{ public void displayMsg() throws IOException{ System.out.println("In ExceptionOverrideDemo displayMsg()"); } }
- subclass can declare the subtype exception of the exception declared in the superclass method.
class Parent{ public void displayMsg() throws IOException{ System.out.println("In Parent displayMsg()"); } } public class ExceptionOverrideDemo extends Parent{ public void displayMsg() throws FileNotFoundException{ System.out.println("In ExceptionOverrideDemo displayMsg()"); } }
Here in super class displayMsg() method throws IOException where as in subclass overridden displayMsg() method throws FileNotFoundException. Since FileNotFoundException is the subtype (Child class) of IOException so no problem here.
- But subclass method can not declare any exception that is up in the hierarchy than the exception declared in the super class method. Here parent class method is throwing IOException whereas in the subclass overridden method is throwing Exception, it will result in compiler error as IOException is the child class of Exception class, thus Exception is up in the hierarchy.
- Subclass overridden method declares no exception. Subclass overridden method can choose to not throw any exception at all even if super class method throws an exception.
class Parent{ public void displayMsg() throws IOException{ System.out.println("In Parent displayMsg()"); } } public class ExceptionOverrideDemo extends Parent{ public void displayMsg(){ System.out.println("In ExceptionOverrideDemo displayMsg()"); } }
That's all for this topic Java Exception Handling And Method Overriding. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
No comments:
Post a Comment