Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

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

user-image
Question added by Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
Date Posted: 2014/05/24
soubhagya kumar bitt
by soubhagya kumar bitt , Technical Lead , IBM india

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 { } }

Yes, we can override that method which throws NullPointerException in superclass and throws Runtime Exception in subclass.

عبد الرحمن أحمد عبد المنعم أحمد
by عبد الرحمن أحمد عبد المنعم أحمد , Army Engineer , Egyptian Armed Forces

Yes.

Firstly, the 'throws' is only meant to be used on checked exceptions.

Both exceptions you mention here are 'RuntimeException's (actually NullPointerException is a subclass of RuntimeException), which are unchecked exceptions. This means that they are not checked at compile time. In fact, you can have your subclass throw a runtime exception even if the super class doesn't throw any exceptions.

Mohamed Aslam Mohamed Ameer
by Mohamed Aslam Mohamed Ameer , Senior Support Consultant , Totalamber Asia Pacific Pvt Ltd

Yes it is possible.NullPointerException is a Runtime Exception

swapnil Shelke
by swapnil Shelke , Senior Developer Analyst , Accenture Services Pvt Ltd

Yes, we can override the method in sub class.

 

Yasir Shabbir Choudhary
by Yasir Shabbir Choudhary , Lead Backend Engineer , FirePhoenix Trading LLC

Yes you can because NullPointerException is a child class of RuntimeException . 

Both are UnChecked Exception .

 

 

 

Yes, we can override that method 

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 RunTime Expection

{}

}

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.

 

Source:-http://tech.queryhome.com/4/nullpointerexception-override-method-runtimeexception

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.