There are many rules if we talk about methodoverriding with exception handling. Which are given below
1. 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.
2. 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.
1. If the superclass method does not declare an exception:
Case 1: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.
For Example:
import java.io.*;
class Test{
void display(){System.out.println("this is super class");}
}
class Student extends Test{
void display()throws IOException{
System.out.println("this is subclass");
}
publi static void main(String []args){
Test t=new Student();
t.display();
}
}
Output: Compile Time Error
Case 2: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.
For Example:
import java.io.*;
class Test{
void display(){System.out.println("this is super class");}
}
class Student extends Test{
void display()throws ArithmeticException{
System.out.println("this is subclass");
}
publi static void main(String []args){
Test t=new Student();
t.display();
}
}
Output: this is subclass
2. If the superclass method declares an exception
Case 1. If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
Example in case subclass overridden method declares parent exception:
import java.io.*;
class Test{
void display() throws ArithmeticException
{
System.out.println("this is super class");
}
}
class Student extends Test{
void display()throws Exception{
System.out.println("this is subclass");
}
publi static void main(String []args){
Test t=new Student();
try{
t.display();
}
catch(Exception ex) { }
}
}
Output: Compile Time Error
Example in case subclass overridden method declares same exception
import java.io.*;
class Test{
void display() throws Exception
{
System.out.println("this is super class");
}
}
class Student extends Test{
void display()throws Exception{
System.out.println("this is subclass");
}
publi static void main(String []args){
Test t=new Student();
try{
t.display();
}
catch(Exception ex) { }
}
}
Output: this is subclass
Example in case subclass overridden method declares subclass exception:
import java.io.*;
class Test{
void display() throws Exception
{
System.out.println("this is super class");
}
}
class Student extends Test{
void display()throws ArithmeticException{
System.out.println("this is subclass");
}
publi static void main(String []args){
Test t=new Student();
try{
t.display();
}
catch(Exception ex) { }
}
}
Output: this is subclass
Example in case subclass overridden method declares no exception:
import java.io.*;
class Test{
void display() throws Exception
{
System.out.println("this is super class");
}
}
class Student extends Test{
void display() {
System.out.println("this is subclass");
}
publi static void main(String []args){
Test t=new Student();
try{
t.display();
}
catch(Exception ex) { }
}
}
Output: this is subclass
No comments:
Post a Comment