Removing throws in overriden method, compiler wants a try/catch block when invoking

后端 未结 3 1520
轻奢々
轻奢々 2021-01-20 14:38

I have a subclass and I am overriding the inherited parent method: I am removing the throws clause from the method declaration.

Now, using polymorphism, the my insta

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-20 14:52

    That has happened because assigning the SubB instance to a removes the knowledge that the instance is a SubB. As an A, a.foo() can now (as far as the JLS is concerned) throw Exception.

    Compile-time type knowledge is based on the declared types of the variables rather than any (hypothetical) inferred types if the variable contents.

提交回复
热议问题