Throw/declare checked and unchecked exception while overriding superclass method in java


Contents of page :
  • If superclass method does not throw/declare any exception >
  • If superclass method throws/declare checked/compileTime exception >
  • If superclass method throws/declare unchecked/RuntimeException >

  • PROGRAMMING TIME >
  • If superclass method does not throw/declare any exception >
    • Program1 to show - overridden method of subclass can declare/throw any unchecked /RuntimeException (superclass or subclass)
    • Program2 to show - overridden method of subclass cannot declare/throw any checked exception
  • If superclass method throws/declare checked/compileTime exception >
    • Program3 to show - overridden method of subclass can declare/throw narrower (subclass of) checked exception
    • Program4 to show - overridden method of subclass cannot declare/throw broader (superclass of) checked exception
    • Program5 to show - overridden method of subclass can declare/throw any unchecked /RuntimeException
  • If superclass method throws/declare unchecked/RuntimeException >
    • Program6 to show - overridden method of subclass can declare/throw any unchecked /RuntimeException
    • Program7 to show - overridden method of subclass cannot declare/throw any checked exception


Newly added programs in article >
If superclass method does not throw/declare any exception >
  • overridden method of subclass may not declare/throw any exception  (As shown in Program 8 - Newly added on reader's request -).

If superclass method throws/declare checked/compileTime exception >
  • overridden method of subclass may not declare/throw any exception. (As shown in Program 9 - Newly added on reader's request).

If superclass method throws/declare unchecked/RuntimeException >
  • overridden method of subclass may not declare/throw any exception (As shown in Program 10 - Newly added on reader's request - so as to avoid any confusion in understanding of basic concepts).



In this post we’ll explore different possibilities of how we can handle checked and unchecked exception while overriding superclass method.


If superclass method does not throw/declare any exception >
  • overridden method of subclass can declare/throw any unchecked /RuntimeException (As shown in Program1), or
  • overridden method of subclass cannot declare/throw any checked exception (As shown in Program2), or
  • overridden method of subclass may not declare/throw any exception  (As shown in Program 8 - Newly added on reader's request - so as to avoid any confusion in understanding of basic concepts)..


If superclass method throws/declare checked/compileTime exception >
  • overridden method of subclass can declare/throw narrower (subclass of) checked exception (As shown in Program3), or
  • overridden method of subclass cannot declare/throw broader (superclass of) checked exception (As shown in Program4), or
  • overridden method of subclass can declare/throw any unchecked /RuntimeException (As shown in Program5), or
  • overridden method of subclass can declare/throw same exception, or
  • overridden method of subclass may not declare/throw any exception. (As shown in Program 9 - Newly added on reader's request - so as to avoid any confusion in understanding of basic concepts).

If superclass method throws/declare unchecked/RuntimeException >
  • overridden method of subclass can declare/throw any unchecked /RuntimeException (superclass or subclass) (As shown in Program1), or
  • overridden method of subclass cannot declare/throw any checked exception (As shown in Program2), or
  • overridden method of subclass can declare/throw same exception, or
  • overridden method of subclass may not declare/throw any exception (As shown in Program 10 - Newly added on reader's request - so as to avoid any confusion in understanding of basic concepts).


PROGRAMMING TIME >

------ If superclass method does not throw/declare any exception >

Program1 to show - overridden method of subclass can declare/throw any unchecked /RuntimeException (superclass or subclass)

If superclass method does not throw/declare any exception - overridden method of subclass can declare/throw any unchecked /RuntimeException (superclass or subclass)

class SuperClass{
   void method(){
          System.out.println("superClass method");
   }
}
class SubClass extends SuperClass{
   void method() throws NullPointerException{
          System.out.println("SubClass method");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class ExceptionTest {
   public static void main(String[] args) throws Exception {
          SuperClass obj=new SubClass();
          obj.method();
   }
}
/*OUTPUT
SubClass method
*/


Program2 to show - overridden method of subclass cannot declare/throw any checked exception


If superclass method does not throw/declare any exception - overridden method of subclass cannot declare/throw any checked exception

Any attempt to throw checked exception in overridden method of subclass will cause compilation error.




----If superclass method throws/declare checked/compileTime exception >

Program3 to show - overridden method of subclass can declare/throw narrower (subclass of) checked exception

If superclass method throws/declare checked/compileTime exception - overridden method of subclass can declare/throw narrower (subclass of) checked exception

IOException is superclass of FileNotFoundException.
import java.io.FileNotFoundException;
import java.io.IOException;
class SuperClass{
   void method() throws IOException{
          System.out.println("superClass method");
   }
}
class SubClass extends SuperClass{
   void method() throws FileNotFoundException{
          System.out.println("SubClass method");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class ExceptionTest {
   public static void main(String[] args) throws Exception {
          SuperClass obj=new SubClass();
          obj.method();
   }
}
/*OUTPUT
SubClass method
*/



Program4 to show - overridden method of subclass cannot declare/throw broader (superclass of) checked exception

If superclass method throws/declare checked/compileTime exception - overridden method of subclass cannot declare/throw broader (superclass of) checked exception

Any attempt to throw broader (superclass of) checked exception in overridden method of subclass will cause compilation error.
Exception is superclass of IOException.

Program5 to show - overridden method of subclass can declare/throw any unchecked /RuntimeException

If superclass method throws/declare checked/compileTime exception - overridden method of subclass can declare/throw any unchecked /RuntimeException
import java.io.IOException;
class SuperClass{
   void method() throws IOException{
          System.out.println("superClass method");
   }
}
class SubClass extends SuperClass{
   void method() throws NullPointerException{
          System.out.println("SubClass method");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class ExceptionTest {
   public static void main(String[] args) throws Exception {
          SuperClass obj=new SubClass();
          obj.method();
   }
}
/*OUTPUT
SubClass method
*/





--------If superclass method throws/declare unchecked/RuntimeException >

Program6 to show - overridden method of subclass can declare/throw any unchecked /RuntimeException

If superclass method throws/declare unchecked/RuntimeException - overridden method of subclass can declare/throw any unchecked /RuntimeException

RuntimeException is superclass of NullPointerException.
class SuperClass{
   void method() throws NullPointerException{
          System.out.println("superClass method");
   }
}
class SubClass extends SuperClass{
   void method() throws RuntimeException{
          System.out.println("SubClass method");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class ExceptionTest {
   public static void main(String[] args) {
          SuperClass obj=new SubClass();
          obj.method();
   }
}
/*OUTPUT
SubClass method
*/


Program7 to show - overridden method of subclass cannot declare/throw any checked exception

If superclass method throws/declare unchecked/RuntimeException - overridden method of subclass cannot declare/throw any checked exception

Any attempt to throw checked exception in overridden method of subclass will cause compilation error.




Let’s see newly added programs >


If superclass method does not throw/declare any exception >
  • overridden method of subclass may not declare/throw any exception  (As shown in Program 8 - Newly added on reader's request - so as to avoid any confusion in understanding of basic concepts)..

class SuperClass{
   void method(){
          System.out.println("superClass method");
   }
}
class SubClass extends SuperClass{
   void method() {
          System.out.println("SubClass method");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class ExceptionTest {
   public static void main(String[] args) throws Exception {
          SuperClass obj=new SubClass();
          obj.method();
   }
}
/*OUTPUT
SubClass method
*/

It’s a valid method overriding program.


If superclass method throws/declare checked/compileTime exception >
  • overridden method of subclass may not declare/throw any exception. (As shown in Program 9 - Newly added on reader's request - so as to avoid any confusion in understanding of basic concepts).

import java.io.FileNotFoundException;
import java.io.IOException;
class SuperClass{
   void method() throws IOException{
          System.out.println("superClass method");
   }
}
class SubClass extends SuperClass{
   void method(){
          System.out.println("SubClass method");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class ExceptionTest {
   public static void main(String[] args) throws Exception {
          SuperClass obj=new SubClass();
          obj.method();
   }
}
/*OUTPUT
SubClass method
*/

It’s a valid method overriding program.


If superclass method throws/declare unchecked/RuntimeException >
  • overridden method of subclass may not declare/throw any exception (As shown in Program 10 - Newly added on reader's request - so as to avoid any confusion in understanding of basic concepts).

class SuperClass{
   void method() throws NullPointerException{
          System.out.println("superClass method");
   }
}
class SubClass extends SuperClass{
   void method(){
          System.out.println("SubClass method");
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class ExceptionTest {
   public static void main(String[] args) {
          SuperClass obj=new SubClass();
          obj.method();
   }
}
/*OUTPUT
SubClass method
*/
It’s a valid method overriding program.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>



eEdit
Must read for you :