title: Saving variable as subclass (interface)
body: <p>I'm making an AsyncTask extension that communicates with a service using different actions and returns different types of results depending on the action used. Each action has a listener, made of interfaces extending one interface called <code>OnCommunicationFailedListener</code>. The thing is, I want to have only one variable to save the listener, <code>OnCommunicationFailedListener</code> type, and make the user be able to save all "sub-interfaces" in that variable. They can save them just as is, but I cannot call the methods that the "sub-interface" introduces.</p>
  
  <pre><code>class CommunicationTask extends AsyncTask&lt;Void, Void, String&gt; {
      private Action action;  
      private OnCommunicationFailedListener listener;
  
      enum Action {
          LOGIN,
          LOGOUT,
          SEARCH
      }
  
      interface OnCommunicationFailedListener {
          void onCommunicationFailed();
      }
  
      interface OnLoginAchievedListener extends OnCommunicationFailedListener {
          void onLoggedIn(String result);
          void onLoginError(String error);
      }
  
      interface OnLogoutAchievedListener extends OnCommunicationFailedListener {
          void onLoggedOut(String result);
          void onLogoutError(String error);
      }
  
      interface OnSearchAchievedListener extends OnCommunicationFailedListener {
          void onSearchAchieved(String[] results);
      }
  
      @Override protected String doInBackground(Void... voids){
          try {
              // Try to communicate with a query, resulting in some result.
              return result;
          } catch(IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override protected void onPostExecute(String result){
          super.onPostExecute(result);
  
          if(result == null) {
              listener.onCommunicationFailed();
              return;
          }
  
          // ALL OF THESE GIVE ERRORS, SINCE THE METHODS DO NOT EXIST ANYMORE
          switch(action){
              case LOGIN:
                  if(&lt;check if result is ok&gt;){
                      listener.onLoggedIn(result);
                  } else listener.onLoginError(&lt;some error&gt;);
                  break;
  
              case LOGOUT:
                  if(&lt;check if result is ok&gt;){
                      listener.onLoggedOut(result);
                  } else listener.onLogoutError(&lt;some error&gt;);
                  break;
  
              case SEARCH:
                  String[] results = &lt;process result&gt;;
                  listener.onSearchAchieved(results);
                  break;
          }
      }
  
      void login(String user, String password, OnLoginAchievedListener listener){
          this.listener = listener;
          action = Action.LOGIN;
  
          //Build the query...
  
          execute();
      }
  
      void logout(OnLogoutAchievedListener listener){
          this.listener = listener;
          action = Action.LOGOUT;
  
          //Build the query...
  
          execute();
      }
  
      void search(String query, OnSearchAchievedListener listener){
          this.listener = listener;
          action = Action.SEARCH;
  
          //Build the query...
  
          execute();
      }
  }
  </code></pre>
  
  <p>The idea is that another class calls the methods <code>login()</code>, <code>logout()</code> or <code>search()</code> and gives them a listener of the action's type. When the communication is finalized and the result is acquired, the action's listener methods are called (in my code, this happens in the <code>switch</code> statement).</p>
  
  <p>The thing is, these methods cannot be called since the passed listener has been casted to <code>OnCommunicationFailedListener</code>.</p>
  
  <p>Is there any way to save a subclass in a variable of its superclass type without being casted to it? I don't want to have actual variables for every type of listener.</p>