• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding method  RSS feed

 
Vidya sagari
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the method in super class declares an exception, Is it a must to declare the same exception or narrower exception to override it in a subclass?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to be "rude" and answer your question with a whole bunch of questions.

Have you tried it?  What do you think the answer is ?  Why?

Can an overriding method declare a checked exception that is NOT declared on its super class?  Why or why not?
 
Vidya sagari
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. My question was whether we could override a method in superclass which throws an exception, without declaring the exception in subclass. I tried some code, sorry for not doing that before. I think No it can't be done. But I couldn't find out why, can you explain. 
 
Uwe Lindenberg
Ranch Hand
Posts: 44
2
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the following example explains it. Let there be

File1:  my.yours.MyException1.java


and
File2:  my.yours.MyException2.java


These are two Exceptions. Furthermore

File3:  my.yours.ParentClass .java


and File4:  my.yours.ChildrenClass .java


two classes.

Try to run the code.
 
Uwe Lindenberg
Ranch Hand
Posts: 44
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason may be the following:

1.) While implementing in java you are using interfaces or parent-classes and their methods.
2.) Doing that it may be necessary to catch some exceptions and to react in a special way like try{...} catch (SomeException ex) {...}
3.) Although you are using interfaces or parent-classes as declaration type for objects in the try-catch, it could be an object from an implementing class or children-class.
4.) Imagine what would happen if the method of a children-class throws a bigger exception. It wouldn't be catched.
5.) So only equal or narrower exceptions are allowed in class that implements an interface or inherits from a parent class.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya sagari wrote:. . . My question was whether we could override a method in superclass which throws an exception, without declaring the exception in subclass. . . .
Are you sure that is the question? Because it appears to me to have a simple answer, viz. yes.Try that.

An overriding method is a feasibility‑preserving refinement, which means that for every instance in which the superclass method is feasible the subclass method is feasible. That means the subclass method must not throw any Exceptions which the superclass method doesn't throw. People will tell you it is all right to throw a new RuntimeException. That is untrue. You must not throw such an Exception (at least I think the code will be incorrect otherwise). The code will compile, but that is because RuntimeExceptions are unchecked and the compiler ignores them. The compiler ignoring an error is different from the code being all right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!