title: Java: If-else instanceof extended classes
body: <p>I have an abstract class <code>X</code> and some classes who extend this class, call them <code>A</code>, <code>B</code> and <code>C</code>.</p>
  
  <p>In some other class <code>Y</code> I have a few methodcalls that depend on the type of the class. The if-else statement looks like this:</p>
  
  <pre><code>public class Y implements InterfaceY {
  
      public Y(){
      }
  
      public String doStuff (X x, Boolean flag) {
      String s = "";
      if (x instanceof A) {
          doStuff((A) x));
      } else if (x instanceof B) {
          doStuff((B) x));
      } else if (x instanceof C) {
          doStuff((C) x, flag); 
      } else {
          throw new Exeption();
      }
      return s;
  
      private String doStuff(A a) {
          return "";
      }
  
      private String doStuff(B b) {
          return "";
      }
  
      private String doStuff(C c, Boolean flag) {
          return "";
      }
  }
  </code></pre>
  
  <p>Note that all methods have the same name (<code>doStuff()</code>) but depending on the class (and sometimes flag) call a different method implementation of that method. Of course this looks horrible and gets immensely complicated once the classed that are extended from X increase.</p>
  
  <p>Is there any way that I can somehow create an intermediate Interface that (or something else) that takes care of the majority (or all) of the if-else statements? </p>