Sunday, June 23, 2019

Java Exception Handling And Method Overriding

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.
    1. sub-class can declare the same exception as declared in the super-class method.
    2. 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.
    3. 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.

Java Exception handing and method overriding

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

  1. 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()"); 
      }  
    }  
    
  2. 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.

  3. But subclass method can not declare any exception that is up in the hierarchy than the exception declared in the super class method.
    Exception & method overriding in Java
    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.
  4. 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

  1. Method Overriding in Java
  2. throw Statement in Java Exception Handling
  3. Multi-Catch Statement in Java Exception Handling
  4. Exception Propagation in Java Exception Handling
  5. Java Exception Handling Interview Questions

You may also like-

  1. Difference between abstract class and interface
  2. strictfp in Java
  3. Marker interface in Java
  4. ConcurrentHashMap in Java
  5. How HashMap internally works in Java
  6. fail-fast Vs fail-safe iterator in Java
  7. Inter-thread communication using wait, notify and notifyAll
  8. Why wait(), notify() and notifyAll() methods are in Object class

No comments:

Post a Comment