Handle exceptions in overriding methods

The question why makes any topic makes more clearer and interesting than just to know about what the concept is. The rule about exceptions when we use them in overriding is:
If super class method is throwing any exception(checked) and if we are overriding that method in sub class.

also read:

Then the subclass method can be declared using following rules

1. The subclass method can throw exception or may ignore the exception, the rule here is quite clear because if super class method is taking risk does not mean that sub class has to take the same risk.

for example in sub class i can write the code which may not be risky at all so we need not to handle any exception or need not to throw any.

below code explains the scenario.
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. If we are throwing any exceptions those should be sub type of exceptions declared in super class method

this rule is very interesting to discuss.

Now i will present some code which is calling Super class method test()

public class Overriding {
   public static void main(String[] args) {
     Super super1 = new Super ();
     try {
       super1.test();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
}

In overriding class i am calling the method test() using object of Super class, here the reference variable super1 is of type Super,and test() method is throwing IOException which is checked exception so either we have to handle it or declare it, here i am handling it with try and catch.

Now i decided to override the test() method is my Sub class by throwing the same exception.

class Super{
   public void test() throws IOException{
     System.out.println("Super.test()");
   }
}

and i will change the way i am creating instance in Overriding class to polymorphic

Super super1 = new Sub();

Here the reference super1 is of type Super but it is actually pointing ti Sub class object

so when we say super1.test()

compiler checks whether test() method is there in super class or not of it is not there it will not compile. We have test() method in Super class and it throws checked exception so we have to surround it with try and catch

try {
super1.test();
} catch (IOException e) {
e.printStackTrace();
}

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()”);
   }
}

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. superb explanation

  2. superb explanation

Speak Your Mind

*