• Defining multiple methods with same name and same signature in super class and sub class known as method overriding.
  • When Overriding a method there is a chance of having statements which may cause exceptions so we need to handle those inside method.
  • Otherwise simply we can give responsibility of handling exceptions to calling method.
  • We can give the responsibility of handling exceptions of a method to calling place by using throws keyword in java.
  • Now we are going to discuss about exception handling in method overriding in java.
  • When am method is overridden in sub class and super class method having throws exception. Then we have some scenarios to discuss.



1.Super class method not throwing any exceptions.
2.Super class method  throws exceptions.


1.Super class method  Not throwing any exceptions.
  • When super class method not throws ant exception,
  • We can add throws unchecked exception in sub class overridden method.
  • We can not add throws checked exception.

Program #1: When super class method does not have any throws exception then we can add throws un checked exception in subclass overridden method.

  1. package exceptionhandlingmethodoverridingjava;
  2. public class Super {
  3.  
  4.     /**
  5.      * @author www.instanceofjava.com
  6.      * @category throws in method overriding java
  7.      */
  8.     
  9.     
  10. public void show(){
  11.         
  12.  System.out.println("Super class show() method");
  13.  
  14. }
  15.  
  16. }

  1. package exceptionhandlingmethodoverridingjava;
  2. public class Sub extends Super {
  3.  
  4. /**
  5. * @author www.instanceofjava.com
  6. * @category throws in method overriding java
  7. */
  8.     
  9. public void show() throws NullPointerException{
  10.         
  11.         System.out.println("Sub class show() method");
  12.  }

  13. public static void main(String[] args) {
  14.  
  15.    Sub obj = new Sub();
  16.     obj.show();
  17.  
  18. }
  19.  
  20. }
Output:

  1. Sub class show() method


Program #2: When super class method does not have any throws exception then we can not add throws checked exception in subclass overridden method.

  1. package exceptionhandlingmethodoverridingjava;
  2. public class Super {
  3.  
  4.     /**
  5.      * @author www.instanceofjava.com
  6.      * @category throws in method overriding java
  7.      */
  8.     
  9.     
  10. public void show(){
  11.         
  12.  System.out.println("Super class show() method");
  13.  
  14. }
  15.  
  16. }

Exception handling in method overriding throws



2.Super class method  throws exceptions.

  • If super class method throws checked exceptions sub class overridden method can throw same exception , sub class exception or no exception but can not declare parent exception.
  • If super class method throws unchecked exceptions then no rules.


Program #3: When super class method  throws checked exception then we can add throws  checked exception in subclass overridden method.

  1. package exceptionhandlingmethodoverridingjava;
  2. public class Super {
  3.  
  4.     /**
  5.      * @author www.instanceofjava.com
  6.      * @category throws in method overriding java
  7.      */
  8.     
  9.     
  10. public void show() throws IOException{
  11.         
  12.  System.out.println("Super class show() method");
  13.  
  14. }
  15.  
  16. }

  1. package exceptionhandlingmethodoverridingjava;
  2. public class Sub extends Super {
  3.  
  4. /**
  5. * @author www.instanceofjava.com
  6. * @category throws in method overriding java
  7. */
  8.     
  9. public void show() throws IOException{
  10.         
  11.         System.out.println("Sub class show() method");
  12.  }

  13. public static void main(String[] args) {

  14.  
  15.    Sub obj = new Sub();
  16.  
  17.  try {
  18.             obj.show();
  19. } catch (IOException e) {
  20.          
  21.             e.printStackTrace();
  22. }
  23.  
  24. }
  25.  
  26. }
Output:

  1. Sub class show() method

Program #4: When super class method  throws unchecked exception then we can not add throws  checked exception in subclass overridden method.

  1. package exceptionhandlingmethodoverridingjava;
  2. public class Super {
  3.  
  4.     /**
  5.      * @author www.instanceofjava.com
  6.      * @category throws in method overriding java
  7.      */
  8.     
  9.     
  10. public void show() throws ArithmeticException{
  11.         
  12.  System.out.println("Super class show() method");
  13.  
  14. }
  15.  
  16. }


throws in method overriding java


Program #5: When super class method  throws checked exception then we can not add throws its  parent exception in subclass overridden method.

  1. package exceptionhandlingmethodoverridingjava;
  2. public class Super {
  3.  
  4.     /**
  5.      * @author www.instanceofjava.com
  6.      * @category throws in method overriding java
  7.      */
  8.     
  9.     
  10. public void show() throws FileNotFoundException{
  11.         
  12.  System.out.println("Super class show() method");
  13.  
  14. }
  15.  
  16. }

  1. package exceptionhandlingmethodoverridingjava;
  2. public class Sub extends Super {
  3.  
  4. /**
  5. * @author www.instanceofjava.com
  6. * @category throws in method overriding java
  7. */
  8.     
  9. public void show() throws IOException{
  10.         
  11.         System.out.println("Sub class show() method");
  12.  }

  13. public static void main(String[] args) {

  14.  
  15. Sub obj = new Sub();
  16.  
  17. try {
  18.            obj.show();
  19. } catch (Exception e) {
  20.           
  21.             e.printStackTrace();
  22. }
  23.  
  24. }
  25.  
  26. }

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu