java - When planning for inheritence, are constructers allowed to call overridable method? -


from effective java 2nd edition, item 17:

for each public or protected method or constructor, documentation must indicate overridable methods method or constructor invokes

later in same item says:

constructors must not invoke overridable methods, directly or indirectly.

aren't these 2 statements contradictory, or missing something?

invoking overridable methods during construction allowed - there nothing illegal this.

invoking overridable methods during construction not advisable - ill-advised invoke overridable methods during construction because can cause incomplete objects exposed , restricts predictability of system.

public class {      final int a;      public a() {         = method();     }      protected int method() {         return 42;     }      @override     public string tostring() {         return "a{" + "a=" + + '}';     }  }  public class b extends {      @override     protected int method() {         system.out.println("this=" + this);         return 96;     }  }  public void test() {     system.out.println("b = " + new b()); } 

note first quote refers documentation, not code. suggest issue use of must when should more appropriate.


Comments