exception with respect to inheritance
case 1:- If base class doesn’t throw any exception but child class throws an unchecked exception.
package govind;
public class Parent {
public void viewName()
{
System.out.println("Govind Ballabh Khan");
}
}
package govind;
public class Child extends Parent
{
public void viewName() throws RuntimeException
{
System.out.println("Java Technology");
}
public static void main(String[] args) {
Parent p=new Child();
p.viewName();
}
}
o/p:-----------------------------------
Java Technology
===============================================================================================================================
case 2: If base class doesn’t throw any exception but child class throws an checked exception
package govind;
public class Parent {
public void viewName()
{
System.out.println("Govind Ballabh Khan");
}
}
package govind;
public class Child extends Parent
{
public void viewName() throws InterruptedException
System.out.println("Java Technology");
}
public static void main(String[] args) {
Parent p=new Child();
p.viewName();
}
}
o/p:------------------------
compile time error
==========================================================================================================================================
case 3:- When base class and child class both throws a checked exception
package govind;
public class Parent {
public void viewName() throws InterruptedException
{
System.out.println("Govind Ballabh Khan");
}
}
package govind;
public class Child extends Parent
{
public void viewName() throws InterruptedException
{
System.out.println("Java Technology");
}
public static void main(String[] args) {
Parent p=new Child();
try {
p.viewName();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
o/p:-----------------------------
Java Technology
==========================================================================================================================================
case :-4 When child class method is throwing border checked exception compared to the same method of base class
package govind;
public class Parent {
public void viewName() throws InterruptedException
{
System.out.println("Govind Ballabh Khan");
}
}
package govind;
public class Child extends Parent
{
public void viewName() throws Exception
{
System.out.println("Java Technology");
}
public static void main(String[] args) {
Parent p=new Child();
try {
p.viewName();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
o/p:---------------------
compile time error
==========================================================================================================================================
No comments:
Post a Comment