Forums Register Login

overriding method????

+Pie Number of slices to send: Send
Can an overriding method have no throws clause if the overridden method does have one?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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....
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 538 times.
Similar Threads
Overriding & Overloading
for overloading..?
Polymorphic Synchronized methods
Regarding overriding
Static method Overriding- confusion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:08:14.