top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+2 votes
3,351 views
If a method throws NullPointerException in super class, can we override it with a method which throws RuntimeException?
posted Sep 14, 2013 by Arvind Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn 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
...