GeeCON Prague 2014*
The moose likes Java in General and the fly likes Method Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Method Overriding " Watch "Method Overriding " New topic
Author

Method Overriding

Soumya Ranjan Mohanty
Ranch Hand

Joined: Mar 07, 2010
Posts: 44
A overridden method in a subclass cannot throw Broader exception than the method in its Superclass ok, my question is can it throw the same exception as in the Super class??


Is it Correct?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Soumya Ranjan Mohanty wrote:A overridden method in a subclass cannot throw Broader exception than the method in its Superclass ok, my question is can it throw the same exception as in the Super class??

Broader means >. Same means =. and since = is not >, one anser is "Yes". Another answer is "try it and see".
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Soumya Ranjan Mohanty wrote:A overridden method in a subclass cannot throw Broader exception than the method in its Superclass ok,


It is not applicable in case of the Exception is Unchecked(Runtime) Exception.Of-course you should not throws Runtime Exception from method signature
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3112
    
  42

Soumya, please UseCodeTags and do NOT use unnecessary font colors and sizes.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Soumya Ranjan Mohanty wrote:A overridden method in a subclass cannot throw Broader exception than the method in its Superclass ok, my question is can it throw the same exception as in the Super class??


Is it Correct?


The overridden method can throw the same Exception or any other Exception which is its subtypes. You cannot throw an Exception that is broader than the declared one and YES as specified above this applies only for the checked Exceptions.

You can read this if it still not clear.

HTH


My Blog SCJP 5 SCWCD 5
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
In an overriding method, the rule of throwing the same or a narrower exception must
be obeyed for all exceptions, even unchecked exceptions. Although the compiler will
not flag the error, it is not okay to thow a broader exception. Existing code may not
handle the broader exception properly.

Jim ... ...


BEE MBA PMP SCJP-6
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
No. Unchecked exceptions are completely unaffected by this rule, as Himanshu Gupta stated.

Since we have people posting conflicting information, rather than simply making another bald assertion, it might be helpful to post a simple demonstration:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

What Jim meant is that although it is legal for the compiler it does violate the Liskov substitution principle - calling code will not expect these new exceptions and therefore will never handle them correctly. But in the case of RuntimeException this isn't always a big problem since most RuntimeExceptions indicate a programmer mistake. IllegalArgumentException, NullPointerException, NoSuchElementException, IllegalFormatException, InputMismatchException - all can be prevented by proper checks.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Another way to say what Rob says is that declaring unchecked Exceptions does violate the Liskov Substitution Principle, but the compiler has no way of verifying unchecked Exceptions.
 
GeeCON Prague 2014
 
subject: Method Overriding