title: Java Inheritance with Lambdas
body: <p>I'm trying to make it so that a superclass (abstract or interface) is the reference type in an ArrayList (which define a null lambda)... and the sub-classes which implement that superclass/interface have different lambda expressions to shadow the superclass's null lambda. But what ends up happening is, calling the sub-class lambda only calls the superclass's null lambda!</p>
  <p>Example code:</p>
  <pre><code>public abstract class AbstractAction {
      Consumer&lt;AbstractAction&gt; behavior = state -&gt; {
          System.out.println(&quot;AbstractAction: do nothing&quot;);
      };
  }
  public class IfTrue extends AbstractAction {
      Boolean var = true;
      Consumer&lt;IfTrue&gt; behavior = state -&gt; {
          if (state.var == true) state.var = false;
          System.out.println(&quot;IfTrue: &quot; + var);
      };
  }
  public class Compare extends AbstractAction {
      Boolean var1 = true;
      Boolean var2 = true;
  
      Consumer&lt;Compare&gt; behavior = state -&gt; {
          if (state.var1 == state.var2) state.var1 = false;
          System.out.println(&quot;IfTrue: &quot; + var1 + &quot; &quot; + var2);
      };
  }
  public class ActionList {
      List&lt;AbstractAction&gt; list = new ArrayList&lt;&gt;();
  
      public void addAction(AbstractAction abstractAction) {
          list.add(abstractAction);
      }
  
      public void runActions() {
          for (AbstractAction a : list) {
              a.behavior.accept(a);
          }
      }
  }
  public class Main {
      public static void main(String[] args) {
          ActionList list = new ActionList();
          list.addAction(new IfTrue());
          list.addAction(new Compare());
          list.runActions();
      }
  }
  </code></pre>
  <p>The Result (shows the AbstractAction lambda is being run, not the sub-classes):</p>
  <pre><code>AbstractAction: do nothing
  AbstractAction: do nothing
  </code></pre>
  <p>But I expected to see:</p>
  <pre><code>IfTrue: true
  Compare: false true
  </code></pre>
  <p>I also tried making an Interface, but then I can't have different numbers of member variables in each subclass, otherwise I get compile time errors that Actionable interface doesn't have those members. But even if I do give the Actionable interface those variables, I get the same result. The Actionable.behavior lambda is run instead of the subclass implementations.</p>
  <pre><code>public interface Actionable {
      Boolean var = true;
      Boolean var1 = true;
      Boolean var2 = true;
  
      Consumer&lt;Actionable&gt; behavior = state -&gt; {
          System.out.println(&quot;AbstractAction: do nothing&quot;);
      };
  }
  </code></pre>
  <p>Is there no way to shadow the AbstractAction or Actionable.behavior lambda? Or am I thinking about this problem all wrong? I really want to be able to swap out Action objects with different behaviors/lambdas to decouple the actions from the execution process itself.</p>