Harshit Rastogi wrote:As per my understanding the compile time error is because
the SuperClass method throws an Exception but the overriden method is not throwing it.
The rule of overriden method says that if a parent method throws an exception then the overriden method can throw same exception or child of the exception or a runtime exception.
So to make the code work you need to add a exception as stated above.
I think you are missing a point here.The overridden method can throw same,narrower(sub-class of the exceptio), runtime exception or
no exception at all.
the SuperClass method throws an Exception but the overriden method is not throwing it.
This is not the reason,read the explanations provided, it clearly explains the exception occurs as the super class reference type is used to invoke the method. There is no exception if sub-class reference var type is used to invoke the method as the overridden method in the sub-class does not throw any excetion at all.
Hope this clears the doubt..