top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

If a method throws NullPointerException in super class, can we override it with a method which throws RuntimeException?

+2 votes
posted Sep 14, 2013 by Arvind Singh

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote

Yes, we can override that method which throws NullPointerException in superclass and throws Runtime Exception in subclass.
Here is an example:

 class First
      void method() throws NullPointerException
       { }
  class Second extends First
      void method() throws RuntimeException
       { }

One of the most important thing is, When you override any method in subclass, that method never throws any checked exception.The overriding method can throws any checked/Runtime exception.
Overridden method in Java shares same name as original method in Java but can only be overridden in sub class. Original method has to be defined inside interface or base class, which can be abstract as well.
When you override a method in Java its signature remains exactly same including return type. JVM resolves correct overridden method based upon object at run-time by using dynamic binding in Java.
Overloaded method can be subject to compile time binding but overridden method can only be bind at run-time. Static method can not be overridden in Java.

answer Sep 14, 2013 by Vinay Shukla

Useful Links with Similar Problem
Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.