Exception handling with method overriding
Method overriding, when used to handle exceptions, is quite uncertain as the compiler cannot understand which definition to use, the superclass or subclass.
There are two important points to remember while handling exceptions using method overriding.
-
If the superclass method does not declare an exception, then the overriding subclass method cannot declare a checked exception, but it can declare an unchecked exception.
-
If the superclass method declares an exception, then the overriding subclass method can declare the same exception, subclass exception or no exception, but it cannot declare a parent exception thrown by the superclass method.
Method overriding for exception handling involves two main cases:
-
When a superclass does not declare an exception
- When a superclass declares an exception
When superclass does not declare an exception
To show that a Subclass can not declare a checked exception
class Person
{
public void display()
{
System.out.println(" I am a Person ");
}
}
class Student extends Person
{
@Override
public void display() throws IOException
{
System.out.println(" I am a Student ");
}
}
class Main
{
public static void main(String[] args)
{
Student s1 = new Student();
s1.display();
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
error: display() in Student cannot override display() in Person
void display() throws IOException {
^
overridden method does not throw IOException
1 error
As shown above, the superclass Person does not throw any exception while the overriding method of subclass Student throws a checked exception.This results in a compilation error as the compiler monitors checked exceptions during compile time. Hence, an overriding subclass method can not throw a checked exception when the overriding superclass method has no exceptions.
To show that a Subclass can declare an unchecked exception
class Person
{
public void display()
{
System.out.println(" I am a Person ");
}
}
class Student extends Person
{
@Override
public void display() throws NullPointerException
{
System.out.println(" I am a Student ");
}
}
class Main
{
public static void main(String[] args)
{
Student s1 = new Student();
s1.display();
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
I am a Student
The NullPointerException is an unchecked exception. Although the overridden method has no exceptions, the overriding subclass method can throw an unchecked exception and run successfully.
When the superclass declares an exception
Case 1: Subclass declares exceptions other than the child exception of the Superclass declared Exception
class Person
{
public void display() throws RuntimeException
{
System.out.println(" I am a Person ");
}
}
class Student extends Person
{
@Override
public void display() throws Exception
{
System.out.println(" I am a Student ");
}
}
class Main
{
public static void main(String[] args)
{
Student s1 = new Student();
s1.display();
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
error: display() in Student cannot override display() in Person
void display() throws Exception {
^
overridden method does not throw Exception
1 error
You can also try this code with Online Java Compiler
Run Code
The Exception thrown by the subclass overriding method is not a child exception of the RuntimeException, so it throws a compile error. Hence, if the overriding method’s exception is not a child exception of the one thrown by the overridden superclass method, then the code does not compile successfully.
Case 2: Subclass declares a child exception of the Superclass declared Exception.
class Person
{
public void display() throws RuntimeException
{
System.out.println(" I am a Person ");
}
}
class Student extends Person
{
@Override
public void display() throws ArithmeticException
{
System.out.println(" I am a Student ");
}
}
class Main
{
public static void main(String[] args)
{
Student s1 = new Student();
s1.display();
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
I am a Student
ArithmeticException is a child exception of RuntimeException, which is thrown by the overridden superclass method. Hence, the compiler does not give any error, and the code executes successfully.
Case 3: Subclass declares no exception
class Person
{
public void display() throws IOException
{
System.out.println(" I am a Person ");
}
}
class Student extends Person
{
@Override
public void display()
{
System.out.println(" I am a Student ");
}
}
class Main
{
public static void main(String[] args)
{
Student s1 = new Student();
s1.display();
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
I am a Student
You can practice by yourself with the help of online java compiler.
The superclass method throws an IOException, while the subclass overriding method does not throw any exception. Similar to the previous case, we get no error on compilation.
Must Read Static Blocks In Java and Hashcode Method in Java
Frequently Asked Questions
Q: What are the differences between method overriding and method overloading?
-
Method overriding is runtime polymorphism, while method overloading is compile-time polymorphism.
-
Parameters, arguments and method signatures need to be the same in method overriding but different in method overloading.
- Final, static or private methods can not be overridden, but they can be overloaded.
Q: What are the types of exceptions in Java?
There are two types of exceptions - Checked and unchecked.
-
Checked exceptions are compile-time exceptions that are checked by the compiler. If not handled, we get a compile error. A few examples of checked exceptions are IOException, ClassNotFoundException, InterruptionException, etc.
- Unchecked Exceptions are not checked by the compiler, and do not throw a compile error on ignoring it. Examples of unchecked exceptions are NullPointerException, Arithmetic Exception, etc.
Key Takeaways
Exception Handling is a common way to resolve runtime errors in a program. Hence, one needs to know and understand how to handle various kinds of exceptions in different scenarios. This blog gives an introduction to method overriding and Exception handling. We have also discussed different cases with examples to understand Exceptions in overriding methods.
Related links:
Checked vs Unchecked Exceptions
Exception Handling with try-catch-finally
Multithreading in Python
Method Overriding in Java
Super Keyword In Java
Java
Check out the official Coding Ninjas Blog site and visit our Library for more.