title: How do I perform a JAVA callback between classes?
body: <p>I am coming from JavaScript, in which callbacks are pretty easy. I am trying to implement them into JAVA, without success.</p>
  
  <p><strong>I have a Parent class:</strong></p>
  
  <pre><code>import java.net.Socket;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  public class Server {
      ExecutorService workers = Executors.newFixedThreadPool(10);
      private ServerConnections serverConnectionHandler;
  
      public Server(int _address) {
          System.out.println("Starting Server...");
          serverConnectionHandler = new ServerConnections(_address);
  
          serverConnectionHandler.newConnection = function(Socket _socket) {
              System.out.println("A function of my child class was called.");
          };
  
          workers.execute(serverConnectionHandler);
  
          System.out.println("Do something else...");
      }
  }
  </code></pre>
  
  <p><strong>Then I have a child class, that is called from the parent:</strong></p>
  
  <pre><code>import java.io.IOException;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public class ServerConnections implements Runnable {
      private int serverPort;
      private ServerSocket mainSocket;
  
      public ServerConnections(int _serverPort) {
          serverPort = _serverPort;
      }
  
      @Override
      public void run() {
          System.out.println("Starting Server Thread...");
  
          try {
              mainSocket = new ServerSocket(serverPort);
  
              while (true) {
                  newConnection(mainSocket.accept());
              }
          } catch (IOException ex) {
              Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      public void newConnection(Socket _socket) {
  
      }
  }
  </code></pre>
  
  <p>What is the right way of implementing the</p>
  
  <pre><code>serverConnectionHandler.newConnection = function(Socket _socket) {
      System.out.println("A function of my child class was called.");
  };
  </code></pre>
  
  <p>part, in the Parent class, which is clearly not correct?</p>