Thursday, 9 February 2017

Exception Handling in method overriding in Java

     In this post, we can discuss the how exception handling in  method overriding in Java. There are some rules to handle the exception in method overriding.

Rule - 1) If super class method does not declare an exception, subclass overridden method can  not declare the checked exception.


   Example : -
                      class Parent {
                               public void display () {
                                       System.out.println("This is the super class method");
                               }

                       }


                       class Child extends parent {
                                public void display () throws FileNotFoundException {      //compile time error
                                       System.out.println("This is the overridden method");
                                }
                         
                                public static void main (String[] args) {
                                        Parent p = new Child ();
                                        p.display();
                                }
                       }

     Output : - Compile Time Error.



Rule - 2)  If super class method does not declare an exception(checked or unchecked), sub class overridden method can not declare checked  exception but declare unchecked exception.


Example : -
                   class Parent {
                               public void display () {
                                       System.out.println("This is the super class method");
                               }
                       }

                       class Child extends parent {
                                public void display () throws NullPointerException {      
                                       System.out.println("This is the overridden method");
                                }
                         
                                public static void main (String[] args) {
                                        Parent p = new Child ();
                                        p.display();
                                }
                       }
               
 Output : - This is the overridden method



Rule - 3) If the super class method declares an exception, sub class overridden method declares same exception, sub class exception or no exception but can not declare parent exception.


Example 1:-
           If the super class method declares an exception , sub class overridden method declares a same exception.
     
               class Parent {
                               public void display () throws NullPointerException {
                                       System.out.println("This is the super class method");
                               }

                       }


                       class Child extends parent {
                                public void display () throws NullPointerException {      
                                       System.out.println("This is the overridden method");
                                }
                         
                                public static void main (String[] args) {
                                        Parent p = new Child ();
                                        p.display();
                                }
                       }

The above code run successfully. And will print "This is the overridden method".

Example 2 :-
            If the super class method declares an exception , sub class overridden method declares a sub class exception .  
                       
                        class Parent {
                               public void display () throws Exception {
                                       System.out.println("This is the super class method");
                               }

                       }


                       class Child extends parent {
                                public void display () throws ArithmeticException {      
                                       System.out.println("This is the overridden method");
                                }
                         
                                public static void main (String[] args) {
                                        Parent p = new Child ();
                                        p.display();
                                }
                       }

The above code run successfully. And will print "This is the overridden method".


Example 3 :-
            If the super class method declares an exception , sub class overridden method declares no exception .

                       class Parent {
                               public void display () throws Exception {
                                       System.out.println("This is the super class method");
                               }

                       }


                       class Child extends parent {
                                public void display () {      
                                       System.out.println("This is the overridden method");
                                }
                         
                                public static void main (String[] args) {
                                        Parent p = new Child ();
                                        p.display();
                                }
                       }

The above code run successfully. And will print "This is the overridden method".

Example 4 :-  
              If the super class method declares an exception, sub class overridden method can not declares parent exception(i.e border exception).

                      class Parent {
                               public void display () throws ArithmeticException {
                                       System.out.println("This is the super class method");
                               }

                       }


                       class Child extends parent {
                                public void display () throws Exception { //Compile Time error 
                                       System.out.println("This is the overridden method");
                                }
                         
                                public static void main (String[] args) {
                                        Parent p = new Child ();
                                        p.display();
                                }
                       }

The above code can not compile, will give Compile Time Error.


Related Post:-
Exception Handling Interview questions and answers  
How to Create the Custom Exception in Java ?  

2 comments:

  1. Tks very much for your post.

    Avoid surprises — interviews need preparation. Some questions come up time and time again — usually about you, your experience and the job itself. We've gathered together the most common questions so you can get your preparation off to a flying start.

    You also find all interview questions at link at the end of this post.

    Source: Download Ebook: Ultimate Guide To Job Interview Questions Answers:

    Best rgs













    .

    ReplyDelete