Brian

25%
Flag icon
Constructors must not invoke overridable methods, directly or indirectly. If you violate this rule, program failure will result. The superclass constructor runs before the subclass constructor, so the overriding method in the subclass will get invoked before the subclass constructor has run. If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.
Brian
Could this be detected at compile time somehow? At least for warning purposes?
Robert
· Flag
Robert
The answer is probably technically "yes," but it's too messy to practically be so (involves code analysis well beyond the compiler's role, I think). If we really want to ensure this would never happen…
Effective Java
Rate this book
Clear rating
Open Preview