--------------------------in OO concepts (Exceptions and overriding) you wrote:-------
--------------------------------------------------------------------------------------------------------------It can throw any exception as long as it is a subclass of any of the exceptions thrown by the overridden method. It can also be a subclass of RuntimeException. eg. If the base class A has: void m1() throws IOException then overriding method in class B can be : void m1() throws FileNotFoundException because FileNotFoundException is a subclass of IOException. The logic here is that if somebody is using class A like this:Now, if this method actually gets an object of class B (which is legal since B is a subclass of A) then the code should not fail. Therefore, any subclass of A must respect the contract established by class A. If B's m1() throws an exception that is not compatible with A's m1(), the above code will fail and so that exception will not be acceptable in the throws clause of B's m1().Code: Select all
void someMethod( A a) { try { a.m1() } catch(IOException) { } }
Now this is a great explanation (from the practical side), but I don't know how it fits in with the fact that "Overriding method can throw any Runtime exception.". Which, if we pass to the method above will also break it, but the compiler of the a.m1() overriding code will not disallow this.