Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding method????

 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can an overriding method have no throws clause if the overridden method does have one?
 
vijay gardiner
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yeah you can do that, but the vice versa would end in compilation error.
If I'm right in interpreting ur doubt, here goes my example :
class A {
public void Test() throws IOException { }
}
class B extends A {
public void Test() {}
}
In the above example, Test() with no exception thrown overrides the Test() which throws exception, which is perfectly alright.
Vijay
This
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is no need to declare the exception declared in the throws class of the overriden method.
From JLS,
A method that overrides or hides another method (�8.4.6), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.

The Restriction is only on not throwing any more checked exception than the original overriden method. Less is acceptable....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic