Exception Handling with Method Overriding in Java

MUKUL JHAMUKUL JHA
3 min read

Method Overriding: Method overriding is a feature that allows a subclass or child class to provide a specific implementation of a superclass method that is already provided by one of its superclass or parent classes.

interface Animal {
    String name();
}
class Dog implements Animal{
    @Override
    public String name() {
        return "Dog";
    }
}

Checked Exception:

Checked Exceptions are the exceptions that are checked at compile time. Checked exceptions are also known as compile-time exceptions.

Checked Exceptions
SQLException, FileNotFoundException, IOException, ClassNotFoundException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException etc

Unchecked Exception:

These are the exceptions that are not checked at compile time. It is not forced by the compiler to either handle or specify the exception. Unchecked exceptions are also known as Runtime exceptions.

Unchecked Exceptions
NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException, StringIndexOutOfBoundsException, UnsupportedOperationException etc.

Method Overriding with Exception

  1. If the superclass method doesn't throw/declare an exception.
interface Animal {
    String name();
}

If the superclass method doesn't throw/declare an exception. Then the subclass overridden method can't throw checked exceptions but can throw unchecked exceptions.

// Possible 
class Dog implements Animal{
    @Override
    public String name() throws RuntimeException {
        return "Dog";
    }
}
// Possible
class Dog implements Animal{
    @Override
    public String name() throws NullPointerException, IndexOutOfBoundsException {
        return "Dog";
    }
}
// Not possible
// CE: 'name()' in 'com.test.Dog' clashes with 'name()' in 'com.test.Animal';
// overridden method does not throw 'java.lang.Exception'
class Dog implements Animal{
    @Override
    public String name() throws Exception {
        return "Dog";
    }
}

  1. If the superclass method does throw/declare an exception.

    Case 1: If the superclass method throws/declares a checked exception.

     interface Animal {
         String name() throws Exception;
     }
    

    If the superclass method does throw/declare a checked exception. Then the subclass overridden method may throw checked, unchecked, or no exceptions.

// Possible: Overriden method with checked exception
class Dog implements Animal{
    @Override
    public String name() throws Exception {
        return "Dog";
    }
}
// Possible: Overriden method with unchecked exception
class Dog implements Animal{
    @Override
    public String name() throws RuntimeException {
        return "Dog";
    }
}

// Possible: Overriden method with checked and uncheked exception 
class Dog implements Animal{
    @Override
    public String name() throws  Exception, RuntimeException{
        return "Dog";
    }
}

// Possible : Overriden method with no exception 
class Dog implements Animal{
    @Override
    public String name() {
        return "Dog";
    }
}

Case 2: If the superclass method throws/declares an unchecked exception.

interface Animal {
    String name() throws RuntimeException;
}

If the superclass method does throw/declare an unchecked exception. Then the subclass overridden method may throw/declare unchecked, or no exceptions but they can't throw checked exceptions.

// Possible: Overriden method with uncheck exception
class Dog implements Animal{
    @Override
    public String name() throws RuntimeException{
        return "Dog";
    }
}

// Possible : Overriden method with no exception
class Dog implements Animal{
    @Override
    public String name() {
        return "Dog";
    }
}

// Not Possible : Overriden method with checked exception.
class Dog implements Animal {
    @Override
    public String name() throws Exception{
        return "Dog";
    }
}

Hope you enjoyed reading!
Your support means a lot. Feel free to like and share if you find it valuable. Thanks for your time! ๐Ÿ™

0
Subscribe to my newsletter

Read articles from MUKUL JHA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

MUKUL JHA
MUKUL JHA