• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overriding

 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4593
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soumya, please UseCodeTags and do NOT use unnecessary font colors and sizes.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic