Dear Ranchers,
Can anyone explain exactly why the following occurs:
//=================================
class Base{
void amethod()throws Exception{}
public static void main(
String[] args){
//Base b = new Sub(); //1
Sub s = new Sub(); //2
s.amethod();
}
}
class Sub extends Base{
void amethod(){}
}
//==================================
The sample code is a subclass overriding the supers method. The Super method throws a checked exception. I am aware of the rules that any subclass overriding methods may throw the same or less but not more checked exceptions than the super method. However in the above example - if I compile as is - it compiles fine. No exception needs to handled or rethrown.
When I uncomment line 1, I get the normal unreported exception compiler error.
If I place a try/catch block with 1 commented the compiler complains no exception is thrown.
My question to all you gurus out there is why do the above occur? Why isnt there consistency?
What is the precise reason for this behaviour?
I have read through the Language Specs but couldn't find anything.
Please help!!
Regards
Stephen Batsas
SCPJ2