title: Java (Android): calling a function from Context without cast
body: <p>First off - I'm rather novice at Java so if the question makes no sense do let me know.</p>
  
  <p>Basically I'm making an Android app which communicates with my web service and so I've made a separate class to deal with the communication, which also includes the AsyncTask (I've removed a lot from the code here just for preview):</p>
  
  <pre><code>public class api {
  
  private String caller = null;
  Context that = null;
  
  api(Context that) {
  
      this.that = that;
      this.caller = that.getClass().getSimpleName();
  
  }
  
  void call(String action) {
  
      /* .... */
  
      }
  
      new back().execute(param1, param2);
  
  }
  
  void callback(String action, String result){
  
      that.callback(action, result);
  
  }
  
  
  
  public class back extends AsyncTask&lt;String, Void, String&gt; {
  
      public String response = null;
  
      protected String doInBackground(String... params) {
  
          response = connection.executeRequest(params[1]);
          return response;
  
      }
  
      protected void onPostExecute(String result) {
  
          callback("a", "b");                 
  
      }
  
  }    
  
  
  }
  </code></pre>
  
  <p>And when I use the class from some part of the app (let's say SomeClass.class), I do:</p>
  
  <pre><code>api WS = new api(this);
  WS.call("....");
  </code></pre>
  
  <p>And it's supposed to execute the function 'callback' which is in SomeClass.
  But the key problem here is this line:</p>
  
  <pre><code>that.callback(action, result);
  </code></pre>
  
  <p>Eclipse makes me add the name of the "caller" class in the cast:</p>
  
  <pre><code>(SomeClass) that.callback(action, result);
  </code></pre>
  
  <p>But that doesn't work for me, because I use the 'api' class from many different classes, so ideally I need to put a variable in the cast. I do get the name of the "caller" class here:</p>
  
  <pre><code>this.caller = that.getClass().getSimpleName();
  //obviously this won't work:
  (this.caller) that.callback(action, result);
  </code></pre>
  
  <p>Is there anyway to do that, or am I doing something fundamentally wrong?</p>
  
  <p>Thank you.</p>