Powered by Blogger.

About Java - Handle Exceptions and Overriding Methods in Java

Method Overriding and Exception:-

The overriding method can declare only exceptions that are either the same class or a subclass of the exception. For example, if the superclass method throws an IOException, then the overriding method of superclass method can throw an IOException, a FileNotFoundException, which is the subclass of IOException, but not Exception, which is the superclass of IOException.

Method Overriding and Exception Example:-

 public class A{
   public void methodA()throws IOException{}
 }

 class B extends A{
   public void methodA() throws EOFException{
     // Legal, because EOFException is the sub class of IOException;
   }
 }

 class C extends A{
   public void methodA() throws Exception{
     // Illegal, because Exception is the super class of IOException;
   }
 }

f a method throws an checked exception , then the overriding method cannot throws new or boarder checked exceptions. if a method throws FileNotFoundExcepti

on , its overriding method cannot throw IOException or Exception , but the overriding method need not throw any exception or can throw unchecked exceptions like RuntimeException.

The overriding method should not reduce the visibility of overridden method. if method is declared as protected , the overriding method must have protected or public access modifier but it cannot be default or private.

The return type of overridden and overriding method must be the same or must be of covariant type

if A is the return type of overridden method. the B is the return type of overriding method then "B is-a A" must be true else it results in a complier error.

class A{

}

interface Inter{

}

class B extends A implements inter {

}

class Base {

public Inter getObject() {

}

public A getResult() {

}

public Inter getX() {

}

public Object toString() {

}

}


class Child extends Base {

public B getObject() {}//ok B is-a inter

public B getResult() {}//ok B is-a A

public A getX() {}//complier error as A is-a Inter is false ie A is not a Inter

public String toString() {}//ok String is-a Object
}


Now let me say why we have to use Same or sub type of Exception in sub class test method declaration.

super1.test() is going to call subclass method in runtime because we are using polymorphic assignment in creating super1 instance.

if sub class method is allowed to throw any super type of exceptions declared in Super class method. that is if we are allowed to throw Exception instead IOException( or its subtype). it will not fit into catch statement since catch is using exception declared in super class method that is IOException.

That is the reason we have to use same exception or its subtype.

Complete Code is Given Below:

public class Overriding {
   public static void main(String[] args) {
     Super super1 = new Sub();
     try {
       super1.test();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
}
class Super{
   public void test() throws IOException{
     System.out.println(”Super.test()”);
   }
}
class Sub extends Super{
   public void test(){
    System.out.println(”Sub.test()”);
   }
}

2 comments:

  1. Pretty good post. I just came across your site and wanted to say that I’ve really enjoyed reading your posts. In any case I’ll be subscribing to your feed and I hope you will keep a good work!Cheer!

    sap online training
    software online training
    sap sd online training
    hadoop online training
    sap-crm-online-training

    ReplyDelete
  2. This is one awesome blog article. Much thanks again.
    I really enjoy the blog.Much thanks again. Really Great.


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete