「Java」Superメソッドで親クラスのメソッドを利用する方法
1.親クラスの定義
package com.arkgame.java.study;
public class ParentMethod {
// 親クラスのメソッド
public String Search() {
String str = "Parent Class Method is called";
return str;
}
}
2.子クラスの定義
package com.arkgame.java.study;
public class ChildMethod extends ParentMethod {
// 子クラスのメソッド
public String chldPrint() {
String strChild = "Child Class Method:";
// 親クラスのメソッドを呼び出す
String target = super.Search();
String result;
result = strChild + target;
return result;
}
}
3.superメソッドで親クラスのメソッドを呼出します
package com.arkgame.java.study;
public class SuperMethodTest {
public static void main(String[] args) {
// 子クラスのオブジェクト作成
ChildMethod childObj = new ChildMethod();
// 子クラスのメソッドを呼び出す
String msg = childObj.chldPrint();
System.out.println(msg);
}
}
実行結果
Child Class Method:Parent Class Method is called