Exception handling with method overriding
There are many rules and conditions in Exception handling with method overriding. Such as what will happen if parent class method is throwing some exception and in child class the overridden method is throwing some other or same exceptions. Let us discuss some of them…
If a method in parent class throws unchecked exceptions
1) If a method in parent class throws an unchecked exception then it is possible that the overridden method in child class do not throw any exception.
class Base
{
//method in base class throwing unchecked exception
void show() throws ArithmeticException
{
System.out.println("Show from Base");
}
}
class Child extends Base
{
//overridden method
void show()
{
System.out.println("Child show");
}
public static void main(String[] s)
{
Child c = new Child();
c.show();
}
}
/*Output
Child Show
*/
2) If a method in parent class throws an unchecked exception then the overridden method in child class cannot throw checked exception
import java.io.*;
class Base
{
//show method throwing unchecked exception
void show() throws ArithmeticException
{
System.out.println("Upar wala show");
}
}
class Child extends Base
{
//overridden method throwing checked exception
void show()throws IOException
{
System.out.println("Child show");
}
public static void main(String[] s)
{
Child c = new Child();
c.show();
}
}
3) If a method in parent class throws an unchecked exception then the overridden method in child class cannot throw parent exception (Exception)
import java.io.*;
class Base
{
//show function throws unchecked exception in base class
void show() throws ArithmeticException
{
System.out.println("Show from Base");
}
}
class Child extends Base
{
//overridden method throwing parent exception
void show()throws Exception
{
System.out.println("Child show");
}
public static void main(String[] s)
{
Child c = new Child();
c.show();
}
}
Output:
4) If a method in parent class throws an unchecked exception then the overridden method in child class can throw any uncheckedexception(parallel)
import java.io.*;
class Base
{
//show method throws unchecked exception
void show() throws ArithmeticException
{
System.out.println("Show From Base");
}
}
class Child extends Base
{
//overridden show method throws unchecked exception(any parallel or same)
void show()throws NullPointerException
{
System.out.println("Child show");
}
public static void main(String[] s)
{
Child c = new Child();
c.show();
}}
/*Output
Child Show
*/
If a method in parent class throws checked exceptions
1) If a method in parent class throws a checked exception then it is possible that the overridden method in child class do not throw any exception.
import java.io.*;
class Base
{
//show method throwing a checked exception
void show() throws IOException
{
System.out.println("Show from Base");
}
}
class Child extends Base
{
void show()
{
System.out.println("Child show");
}
public static void main(String... s)
{
Child c = new Child();
c.show();
}}
/*Output
Child Show
*/
2) If a method in parent class throws a checked exception then it is possible that the overridden method in child class can throw any unchecked exception.
import java.io.*;
class Base
{
void show() throws IOException
{
System.out.println("Upar wala show");
}}
class Child extends Base
{
void show() throws ArithmeticException
{
System.out.println("Child show");
}
public static void main(String[] s)
{
Child c = new Child();
c.show();
}}
/*Output
Child Show
*/
3) If a method in parent class throws a checked exception then the overridden method in child class cannot throw parent exception (Exception).
import java.io.*;
class Base
{
//throws checked exception
void show() throws IOException
{
System.out.println("show from Base");
}}
class Child extends Base
{
//throws parent exception
void show() throws Exception
{
System.out.println("Child show");
}
public static void main(String[] s)
{
Child c = new Child();
c.show();
}}
Output:
4) If a method in parent class throws a checked exception then the overridden method in child class can throw the same checked exception.
import java.io.*;
class Base
{
//throws checked exception
void show() throws IOException
{
System.out.println("show from Base");
}}
class Child extends Base
{
//throws same checked exception
void show() throws IOException
{
System.out.println("Child show");
}
public static void main(String[] s)throws Exception
{
Child c = new Child();
c.show();
}}
/*Output
Child Show
*/
5) If a method in parent class throws a checked exception then the overridden method in child class can throw any checked exception(Same last example).
6) If a method in parent class throws a parent exception then the overridden method in child class can throw any unchecked exception.
import java.io.*;
class Base
{
//show throws parent
void show() throws Exception
{
System.out.println("Show from Base");
}}
class Child extendsBaseCheckedToOtherExceptions
{
//overridden show throws any unchecked
void show() throws ArithmeticException
{
System.out.println("Child show");
}
public static void main(String... s)
{
Child c = new Child();
c.show();
}}
/*Output
Child Show
*/
No comments:
Post a Comment