and the answer is "method1 in class NewBase does not need to specify any exceptions", but as I know the overriding method must not thow new or broader checked exception that were declared by the overriden method, but at least it have to declare (or throw) the same exception or it's subclass? So in the above code we have to declare at least IOException or it's subclass, haven't it? Thanks in advance.
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. More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. If n has a throws clause that mentions any checked exception types, then m must have a throws clause, and for every checked exception type listed in the throws clause of n, that same exception class or one of its superclasses must occur in the throws clause of m; otherwise, a compile-time error occurs.
To break that down, an overriding method can throw any subset of checked exceptions declared by the overridden method. That subset can include the entire set (meaning that the overriding method can throw all of the exceptions the overridden method can throw) and it can also include the empty set (meaning that the overriding method throws no expceptions whatsoever). The key is that the overriding method can not throw a checked exception that the overridden method does not declare. I hope that helps, Corey
In case 1, the compiler knows that method1 might throw IOException In case 2, at runtime method1 (the overriding version in NewBase) doesn't throw IOException, however at compile time the decision has to be made based on the reference type, ie Base's method1 signature. So, if the runtime class is a subclass of the parent, and the subclass's overriding method was allowed to throw newer or broader exceptions then common exception handling based on the parent reference's method signature would not be possible. Case 3 illustrates this scenario. The crucial point is that using a base class reference and providing the runtime via any subclass is a powerful paradigm and would break if this exception handling rule wasn't enforced. - ortimuS [ April 06, 2004: Message edited by: ortimus tilap ]