简体   繁体   中英

How do you call a subclass method from a superclass in Java?

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?

Basically what I'm looking to do is this: I have a method called exec that takes a String as a parameter for a command. I want to be able to call the exec method in the subclass that the developer has overridden from the superclass without knowing the subclasses name ahead of time.

This is like the way the Thread class works. I'm not looking to do what every answer I've found does which is Superclass object = new Subclass(); and then just call object.method(); .

This is the code in the superclass

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) -> {
            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) -> {
            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(() -> {
            output.setText(output.getText() + "\n" + value);
        });
    }
    protected abstract void exec(String command);
}

[...] I have a method called exec that takes a String as a parameter for a command. I want to be able to call the exec method in the subclass that the developer has overridden from the superclass without knowing the subclasses name ahead of time.

You don't have to do anything special. You just call it from the code of the base class. All methods in Java are virtual, so the overriding implementation of the subclass will be invoked.

Here's a demo:

public class Test {
    public static void main(String[] args) {
        Console console = new ConsoleSubclass();
        console.start();
        console.keyPressed();
    }
}

abstract class Console {

    Runnable keyPressedHandler;

    private void setOnKeyPressed(Runnable handler) {
        keyPressedHandler = handler;
    }

    public void keyPressed() {
        keyPressedHandler.run();
    }

    public void start() {
        setOnKeyPressed(() -> {
            exec();
        });
    }

    abstract void exec();
}

class ConsoleSubclass extends Console {
    @Override
    void exec() {
        System.out.println("In ConsoleSubclass");
    }
}
public class UsingClass

{
  SuperClassType x = null;
  x = goGetClassReference(); // this can get an instance of SuperClassType,
                             // or of any subclass of it.
  x.methodName();  // this calls methodName on the class obtained above; 
        //if goGetClassReference got a subclass, this calls methodName() on the subclass.
}

The answer for my specific JavaFX subclassing question is over here Subclassing a JavaFX Application . For general subclassing the answers here are quite adequate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM