The moose likes Beginning Java and the fly likes overriding method???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding method????" Watch "overriding method????" New topic

overriding method????

raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
Can an overriding method have no throws clause if the overridden method does have one?
vijay gardiner

Joined: Jan 26, 2002
Posts: 14
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.
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
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....
I agree. Here's the link:
subject: overriding method????
It's not a secret anymore!