SCJP - override method with exception handling raises a compiler error

前端 未结 2 367
旧巷少年郎
旧巷少年郎 2021-01-25 04:43

In the SCJP book by Kathey Sierra, an excerpt is as follows:


If a method is overridden but you use a polymorphic (supertype) reference to refer to

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 05:07

    The compiler doesn't know the real type of the object referenced. It only checks that assignments are valid.

    The call to a.eat causes a compile error because the compiler doesn't know the Animal referenced by a is a Dog2. It is deciding whether a checked exception can be thrown based solely on the type of the variable referencing the Dog2.

    The compiler is not smart. It does not run the code and it does not keep track of the actual class of the object assigned to the variable a.

    Just as when you refer to an object with the type of its superclass you won't see methods specific to the subclass, you also see the throws-clause of the superclass, not that of the subclass's method.

    If you add a cast to the subclass in the last line:

    ((Dog2)a).eat();
    

    then you won't get the unreported exception compile error.

提交回复
热议问题