class Father {
Father() { System.out.println("Father 無參構造器"); }
}
class Son extends Father {
Son() { /* 無顯式 super() */ }
}
new Son(); // 控制台輸出: "Father 無參構造器"

原因:子類構造器第一行隱式插入 super(),調用父類無參構造器。

super(args)super() (隱式插入)錯誤案例:父類只有 Father(String name),子類無顯式 super → 編譯錯誤

兩種解決方案:
super("參數") 匹配父類有參構造器

當子類覆寫父類方法/定義同名屬性時,用 super 訪問被遮蔽的父類版本。
class Father { void show() { System.out.println("Father show"); } }
class Son extends Father {
@Override void show() {
System.out.println("Son show");
super.show(); // 調用父類版本
}
}
new Son().show();
// 輸出:
// Son show
// Father show

| 關鍵字 | 指向 | 構造器調用 | 方法/屬性訪問 |
|---|---|---|---|
| this | 當前對象 | this() 調用本類其他構造器 | this.field / this.method() 訪問當前對象成員 |
| super | 父類部分 | super() 調用父類構造器 | super.field / super.method() 訪問父類被遮蔽成員 |
this(...) 或 super(...) (二擇一)super()Object()