title: How do you call a subclass method from a superclass in Java?
body: <p>I've looked around here find an answer to my question and I can't. How do you call a subclass method from a superclass in Java? </p>
  
  <p>Basically what I'm looking to do is this: I have a method called <code>exec</code> that takes a <code>String</code> as a parameter for a command. I want to be able to call the <code>exec</code> method in the subclass that the developer has overridden from the superclass without knowing the subclasses name ahead of time. </p>
  
  <p>This is like the way the <code>Thread</code> class works. I'm not looking to do what every answer I've found does which is <code>Superclass object = new Subclass();</code> and then just call <code>object.method();</code>.</p>
  
  <p>This is the code in the superclass</p>
  
  <pre><code>import javafx.application.*;
  import javafx.stage.*;
  import javafx.scene.*;
  import javafx.scene.control.*;
  import javafx.scene.input.*;
  import javafx.scene.text.Text;
  
  
  
  public abstract class Console extends Application {
      private String title;
      private static Text output = new Text();
  
  
      public void create(String title) {
          this.title = title;
          launch();
      }
  
      public void start(Stage stage) {
          stage.setOnCloseRequest((WindowEvent event) -&gt; {
              System.exit(0);
          });
          stage.setTitle(title);
          stage.setResizable(false);
          Group root = new Group();
          Scene scene = new Scene(root, 800, 400);
          stage.setScene(scene);
          ScrollPane scroll = new ScrollPane();
          scroll.setContent(output);
          scroll.setMaxWidth(800);
          scroll.setMaxHeight(360);
          TextField input = new TextField();
          input.setLayoutX(0);
          input.setLayoutY(380);
          input.setPrefWidth(800);
          scene.setOnKeyPressed((KeyEvent event) -&gt; {
              if(event.getCode() == KeyCode.ENTER) {
                  exec(input.getText());
                  input.clear();
              }
          });
          root.getChildren().add(scroll);
          root.getChildren().add(input);
          stage.show();
      }
      public static void appendOutput(String value) {
           Platform.runLater(() -&gt; {
              output.setText(output.getText() + "\n" + value);
          });
      }
      protected abstract void exec(String command);
  }
  </code></pre>