title: Java overload: how to call more specific one
body: <p>My program parses <code>WebAssembly</code> instructions and makes decisions based on the context of the current instruction. So, the MWE for my algorithm looks like this:</p>
  
  <pre><code>public class Main {
      public interface Context {
          String name();
      }
      static class M implements Context {
          public String name() {
              return "Context M: ";
          }
      }
      static class N implements Context {
          public String name() {
              return "Context N: ";
          }
      }
  
      public interface Instruction {
          int getId();
  
          String run();
      }
  
      static class A implements Instruction {
          public int getId() {
              return 0;
          }
  
          public String run() {
              return "The work of A";
          }
      }
  
      static class B implements Instruction {
          public int getId() {
              return 1;
          }
  
          public String run() {
              return "The work of B";
          }
      }
  
      static void work(Context context, Instruction instruction) {
          switch (instruction.getId()) {
              case 0:
                  workOnId0(context, (A) instruction);
                  break;
              case 1:
                  workOnId1(context, (B) instruction);
                  break;
              default:
                  throw new RuntimeException("Failed to recognize instruction");
          }
      }
  
      static void workOnId0(Context context, A instruction) {
          System.out.println(context.name() + instruction.run());
      }
  
      static void workOnId1(Context context, B instruction) {
          System.out.println(context.name() + instruction.run());
      }
  
      static void workOnId1(N context, B instruction) {
          System.out.println("This is corner case logic for this context!");
      }
  
      public static void main(String[] args) {
          N context = new N();
          B instruction = new B();
  
          work(context, instruction);
      }
  }
  </code></pre>
  
  <p>As you can see from above, when my instruction is <code>B</code>, then the ordinary work should happen in <code>workOnId1</code>, but in case my context is specifically <code>N</code>, I would like some special work done, which is represented by a different overload of <code>workOnId1</code>.</p>
  
  <p>Unfortunately, the special overload never gets called. How can I make the overload resolution work? </p>