I have a subclass and I override the inherited parent method: I remove the throws clause from the method declaration.
Now, using polymorphism, the execution type of the instance of my instance should determine the implementation of the method; however, the compiler complains and wants to block try / catch around the method call when trying to compile, as if the superclass method was called, and not the version of the subclass?
I know that I can remove the throws declaration or narrow down the thrown exception that can be thrown when overriding. Why is this still throwing an exception?
class A{
void foo()throws Exception{throw new Exception();}
}
class SubB extends A{
@Override
void foo(){ System.out.println("B");
}
public static void main(String[] args) {
A a = new SubB();
a.foo();
}
source
share