'java: A confusion about calling super method in the child class
The parent class and the child class is as follows.
public abstract class Dialog {
public void render() {
// do something
System.out.println(this.getClass());
Button okButton = createButton();
// for example, click "ok" button to close a dialog.
okButton.onClick();
okButton.render();
}
public Button createButton() {
System.out.println("I'm a Button");
return null;
}
}
public class WindowsDialog extends Dialog{
@Override
public Button createButton() {
// do something
System.out.println("I'm windows button");
return new WindowsButton();
}
@Override
public void render() {
super.render();
System.out.println("I'm in the child class");
}
public static void main(String[] args) {
new WindowsDialog().render();
}
}
The execution result is:
class com.pattern.design.behavioral.method.factory.WindowsDialog
I'm windows button
I'm in the child class
I thought the invocation of createButton is I'm a button but it was not.
I want to know why the this.getClass is WindowsDialog and the invocation of 'createButton()' is not I'm a Button.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
