Ambiguity arises when method overriding and exception handling are combined. Which definition should be followed confuses the compiler.
It has two different kinds of issues, which are as follows:
Two scenarios that will occur in this issue are as follows:
Let’s talk about the two cases mentioned above and interpret them using the following examples:
Case 1: If the SuperClass doesn’t declare any exceptions and the checked exceptions are declared by the subclass.
import java.io.*;
class SuperClass {
// SuperClass doesn't declare any exception
void method() {
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// method() declaring Checked Exception IOException
void method() throws IOException {
// IOException is of type Checked Exception
// so the compiler will give Error
System.out.println("SubClass");
}
// Driver code
public static void main(String args[]) {
SuperClass s = new SubClass();
s.method();
}
}
void method() throws IOException {
^
overridden method does not throw IOException
1 error
Case 2: In the event that SuperClass doesn’t declare any exceptions and SubClass does
import java.io.*;
class SuperClass {
// SuperClass doesn't declare any exception
void method()
{
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// method() declaring Unchecked Exception ArithmeticException
void method() throws ArithmeticException
{
// ArithmeticException is of type Unchecked Exception
// so the compiler won't give any error
System.out.println("SubClass");
}
// Driver code
public static void main(String args[])
{
SuperClass s = new SubClass();
s.method();
}
}
SubClass
Now let’s focus on the subsequent issue that arises from that, which occurs if The SuperClass declares an exception. Three cases will present themselves in this issue:
import java.io.*;
class SuperClass {
// SuperClass declares an exception
void method() throws RuntimeException {
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// SubClass declaring an exception
// which are not a child exception of RuntimeException
void method() throws Exception {
// Exception is not a child exception
// of the RuntimeException
// So the compiler will give an error
System.out.println("SubClass");
}
// Driver code
public static void main(String args[]) {
SuperClass s = new SubClass();
s.method();
}
}
void method() throws Exception {
^
overridden method does not throw Exception
1 error
import java.io.*;
class SuperClass {
// SuperClass declares an exception
void method() throws RuntimeException
{
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// SubClass declaring a child exception
// of RuntimeException
void method() throws ArithmeticException
{
// ArithmeticException is a child exception
// of the RuntimeException
// So the compiler won't give an error
System.out.println("SubClass");
}
// Driver code
public static void main(String args[])
{
SuperClass s = new SubClass();
s.method();
}
}
SubClass
import java.io.*;
class SuperClass {
// SuperClass declares an exception
void method() throws IOException
{
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// SubClass declaring without exception
void method()
{
System.out.println("SubClass");
}
// Driver code
public static void main(String args[])
{
SuperClass s = new SubClass();
try {
s.method();
} catch (IOException e) {
e.printStackTrace();
}
}
}
SubClass
Note: also read about the Interface vs Abstract class
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…
Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…
A unival tree (short for "universal value tree") is a tree in which all nodes…
Problem Statement (Asked By Facebook) Given the mapping a = 1, b = 2, ...,…