title: Java - (Anonymous subclass) overriding method during object instance construction
body: <p>I am maintaining some Java 8 code which looks like this:</p>
  
  <pre><code>Class Entity  {
     protected Model theModel;
  
     public Entity()  {
         init();
     }
  
     protected void init()  {
         this.theModel = new Model();
     }
  }
  
  Class Model  {
  }
  
  Class SubModel extends Model {
  }
  
  main {
      Entity newEntity = new Entity()  {
          @Override
          protected void init()  {
              this.theModel = new SubModel();
          }
      };
  }
  </code></pre>
  
  <p>The code currently compiles and runs correctly, but I now need to update it.</p>
  
  <p>My question are:</p>
  
  <ol>
  <li>How is the override of the <code>init()</code> method working at all, during the construction of the <code>newEntity</code>?</li>
  <li>What is the correct terminology for this method override included in the object constructor statement?</li>
  </ol>
  
  <p>My research so far suggests that Java cannot dynamically override methods - cannot do overrides on this basis, because method overrides are per-class not per-object.  But this code snippet seems to show that Java can do it in practice?</p>
  
  <hr>
  
  <p>UPDATE: Note that the creation of the <code>newEntity</code> in <code>main</code> creates an anonymous sub-class, and the <code>init()</code> method is being overridden for that anonymous sub-class only.  This is explained better in the two excellent answers below.</p>