Can we override methods with same signature but in child class it is throwing exception?

Interview - Product Companies, eCommerce Companies, Investment Banking, Healthcare Industry, Service Companies and Startups.

Last updated 1 week ago

There are few things to remember when overriding a method with exception handling because method of super class may have exception declared. In this scenario, there can be possible two cases: either method of super class can declare exception or not.

If super class method declared exception then the method of sub class may declare same exception class, sub exception class or no exception but can not parent of exception class.

For example, if a method of super class declared ArithmeticException then method of subclass may declare ArithmeticException, its subclass or no exception but cannot declare its super/parent class like: Exception class.

In other scenario, if super class method does not declare any exception, then sub class overriden method cannot declare checked exception but it can declare unchecked exceptions. Lets see an example.

Method overriding in Subclass with Checked Exception

Checked exception is the exception which is expected or known to occur at compile time hence they must be handled at compile time.

import java.io.*; class Super { void show() { System.out.println("parent class"); } } public class Sub extends Super { void show() throws IOException //Compile time error { System.out.println("parent class"); } public static void main(String[] args) { Super s=new Sub(); s.show(); } }

In the example above, the method show() doesn't throw any exception when its declared/defined in the Super class, hence its overridden version in the class Sub also cannot throw any checked exception.

If we try to do so, we will get a compile time error.

Method overriding in Subclass with UnChecked Exception

Unchecked Exceptions are the exception which extend the class RuntimeExeption and are thrown as a result of some runtime error.

import java.io.*; class Super { void show() { System.out.println("parent class"); } } class Sub extends Super { void show() throws ArrayIndexOutOfBoundsException { System.out.println("child class"); } public static void main(String[] args) { Super s = new Sub(); s.show(); } }

child class

Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overrided show() method can throw it.

More about Overriden Methods and Exceptions

If Super class method throws an exception, then Subclass overriden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.

It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).


Example of Subclass overriden method with same Exception

Method of a sub class can declare same exception as declared in the super class. See the below example.

import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Sub extends Super { void show() throws Exception //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }

child class

Example of Subclass overriden method with no Exception

It is optional to declare the exception in sub class during overriding. If method of super class declared an exception then it is upto the subclass to declare exception or not. See the below example.

import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Sub extends Super { void show() //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }

child class

Example of Subclass overriden method with parent Exception

It is not allowed to declare parent class exception in the subclass method, we get compile time error if we try to compile that program. See the below example.

import java.io.*; class Super { void show() throws ArithmeticException { System.out.println("parent class"); } } public class Sub extends Super { void show() throws Exception //Compile time Error { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }

Compile time error

There are many rules if we talk about method overriding with exception handling.

Some of the rules are listed below:

  • If the superclass method does not declare an exception
    • If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
  • If the superclass method declares an exception
    • If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

If the superclass method does not declare an exception

Rule 1: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.

Let's consider following example based on the above rule.

TestExceptionChild.java

import java.io.*; class Parent{ // defining the method void msg() { System.out.println("parent method"); } } public class TestExceptionChild extends Parent{ // overriding the method in child class // gives compile time error void msg() throws IOException { System.out.println("TestExceptionChild"); } public static void main(String args[]) { Parent p = new TestExceptionChild(); p.msg(); } }

Output:

Can we override methods with same signature but in child class it is throwing exception?

Rule 2: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.

TestExceptionChild1.java

import java.io.*; class Parent{ void msg() { System.out.println("parent method"); } } class TestExceptionChild1 extends Parent{ void msg()throws ArithmeticException { System.out.println("child method"); } public static void main(String args[]) { Parent p = new TestExceptionChild1(); p.msg(); } }

Output:

Can we override methods with same signature but in child class it is throwing exception?

If the superclass method declares an exception

Rule 1: If the superclass method declares an exception, subclass overridden method can declare the same subclass exception or no exception but cannot declare parent exception.

Example in case subclass overridden method declares parent exception

TestExceptionChild2.java

import java.io.*; class Parent{ void msg()throws ArithmeticException { System.out.println("parent method"); } } public class TestExceptionChild2 extends Parent{ void msg()throws Exception { System.out.println("child method"); } public static void main(String args[]) { Parent p = new TestExceptionChild2(); try { p.msg(); } catch (Exception e){} } }

Output:

Can we override methods with same signature but in child class it is throwing exception?

Example in case subclass overridden method declares same exception

TestExceptionChild3.java

import java.io.*; class Parent{ void msg() throws Exception { System.out.println("parent method"); } } public class TestExceptionChild3 extends Parent { void msg()throws Exception { System.out.println("child method"); } public static void main(String args[]){ Parent p = new TestExceptionChild3(); try { p.msg(); } catch(Exception e) {} } }

Output:

Can we override methods with same signature but in child class it is throwing exception?

Example in case subclass overridden method declares subclass exception

TestExceptionChild4.java

import java.io.*; class Parent{ void msg()throws Exception { System.out.println("parent method"); } } class TestExceptionChild4 extends Parent{ void msg()throws ArithmeticException { System.out.println("child method"); } public static void main(String args[]){ Parent p = new TestExceptionChild4(); try { p.msg(); } catch(Exception e) {} } }

Output:

Can we override methods with same signature but in child class it is throwing exception?

Example in case subclass overridden method declares no exception

TestExceptionChild5.java

import java.io.*; class Parent { void msg()throws Exception{ System.out.println("parent method"); } } class TestExceptionChild5 extends Parent{ void msg() { System.out.println("child method"); } public static void main(String args[]){ Parent p = new TestExceptionChild5(); try { p.msg(); } catch(Exception e) {} } }

Output:

Can we override methods with same signature but in child class it is throwing exception?

Next TopicCustom Exception

Can we override methods with same signature but in child class it is throwing exception?
For Videos Join Our Youtube Channel: Join Now

  • Send your Feedback to [email protected]
Can we override methods with same signature but in child class it is throwing exception?
Can we override methods with same signature but in child class it is throwing exception?
Can we override methods with same signature but in child class it is throwing exception?