• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Handle exceptions in overriding methods

September 17, 2008 //  by Krishna Srinivasan//  Leave a Comment

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:

  • New Features in Java 5.0
  • Generics in Java 5.0
  • Annotations in Java 5.0

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

Category: JavaTag: Core Java

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.

Previous Post: « Request Lifecycle in Struts 2.0 applications
Next Post: Generic Methods in Java 5.0 »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact