title: How to overload a method using anonymous classes in Java?
body: <p>I have a class, Action, that has a method called doAction(). This method exists solely to be overwritten for each new action that I add to an action list called allActions.</p>
  
  <pre><code>public void doAction() {
      System.out.println("Overload this method.");
  } 
  </code></pre>
  
  <p>The list of actions is being stored in the following list:</p>
  
  <pre><code>public static List&lt;Action&gt; allActions = new ArrayList&lt;Action&gt;();
  </code></pre>
  
  <p>and added to this list with calls like the following:</p>
  
  <pre><code>allActions.add(
      new Action(id){
          public void doAction(Word w1, Word w2) {
              //perform some action
          }
      }
  );
  </code></pre>
  
  <p>But this code isn't working for me. When I try to access an overwritten doAction() method from the allActions list, it just performs the default method.</p>
  
  <p>I suspect that the problem is that allActions is a list of Action objects, so perhaps the type is being changed away from the anonymous class when it is being added to the list, and reverting to the default method.</p>
  
  <p>Any ideas on how to maintain a list of different anonymous versions of the Action class? I want to be able to call these different doAction() methods based on the id given to the actions. </p>
  
  <p>By the way, this is my first time encountering anonymous classes. I <em>think</em> I understand the concept, but actually using them is another story.</p>