「Java」親子クラス継承のコンストラクタ(constructor)とメソッド(method)を呼び出す方法

2020年10月28日

説明
1.引数なしコンストラクタ
2.引数ありコンストラクタ
3.Override(オーバーライド)メソット

親クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class ParentSampleClass {
public ParentSampleClass() {
System.out.println("Parent class 引数なし");
}
// コンストラクタ 引数あり
public ParentSampleClass(String username) {
System.out.println("Parent class 引数あり");
}
protected void protectedMthod() {
System.out.println("parentc class protected method is called");
}
public void pubMethod() {
System.out.println("parentc class public method is called");
}
}
package com.arkgame.study; public class ParentSampleClass { public ParentSampleClass() { System.out.println("Parent class 引数なし"); } // コンストラクタ 引数あり public ParentSampleClass(String username) { System.out.println("Parent class 引数あり"); } protected void protectedMthod() { System.out.println("parentc class protected method is called"); } public void pubMethod() { System.out.println("parentc class public method is called"); } }
package com.arkgame.study;

public class ParentSampleClass {

      public ParentSampleClass() {

            System.out.println("Parent class 引数なし");
      }

      // コンストラクタ 引数あり
      public ParentSampleClass(String username) {

            System.out.println("Parent class 引数あり");
      }

      protected void protectedMthod() {
            System.out.println("parentc class protected method is called");
      }

      public void pubMethod() {
            System.out.println("parentc class public method is called");
      }

}

子クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class ChlidSampleClass extends ParentSampleClass {
// コンストラクタ 引数なし
public ChlidSampleClass() {
System.out.println("Child Class 引数なし");
}
// コンストラクタ 引数あり
public ChlidSampleClass(String target) {
System.out.println("Child Class 引数あり");
}
public void pubmethod() {
System.out.println("chlid pubmethod is called");
}
}
package com.arkgame.study; public class ChlidSampleClass extends ParentSampleClass { // コンストラクタ 引数なし public ChlidSampleClass() { System.out.println("Child Class 引数なし"); } // コンストラクタ 引数あり public ChlidSampleClass(String target) { System.out.println("Child Class 引数あり"); } public void pubmethod() { System.out.println("chlid pubmethod is called"); } }
package com.arkgame.study;

public class ChlidSampleClass extends ParentSampleClass {

      // コンストラクタ 引数なし
      public ChlidSampleClass() {
            System.out.println("Child Class 引数なし");

      }

      // コンストラクタ 引数あり
      public ChlidSampleClass(String target) {
            System.out.println("Child Class 引数あり");
      }

      public void pubmethod() {
            System.out.println("chlid pubmethod is called");
      }
}

子クラスのオブジェクトは親クラスを利用する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class PachildConstDemo {
public static void main(String[] args) {
System.out.println("********コンストラクタ 引数なし オブジェクト**************");
// コンストラクタ 引数なし
ChlidSampleClass csA = new ChlidSampleClass();
// 親クラスのpublicメソッド
csA.pubMethod();
// 子クラスのメソッド
csA.pubmethod();
// 親クラスのprotectedメソッド
csA.protectedMthod();
System.out.println("********コンストラクタ 引数あり オブジェクト**************");
// コンストラクタ 引数あり
ChlidSampleClass csB = new ChlidSampleClass("AB88");
// 親クラスのpublicメソッド
csB.pubMethod();
// 子クラスのメソッド
csB.pubmethod();
// 親クラスのprotectedメソッド
csB.protectedMthod();
}
}
package com.arkgame.study; public class PachildConstDemo { public static void main(String[] args) { System.out.println("********コンストラクタ 引数なし オブジェクト**************"); // コンストラクタ 引数なし ChlidSampleClass csA = new ChlidSampleClass(); // 親クラスのpublicメソッド csA.pubMethod(); // 子クラスのメソッド csA.pubmethod(); // 親クラスのprotectedメソッド csA.protectedMthod(); System.out.println("********コンストラクタ 引数あり オブジェクト**************"); // コンストラクタ 引数あり ChlidSampleClass csB = new ChlidSampleClass("AB88"); // 親クラスのpublicメソッド csB.pubMethod(); // 子クラスのメソッド csB.pubmethod(); // 親クラスのprotectedメソッド csB.protectedMthod(); } }
package com.arkgame.study;

public class PachildConstDemo {

      public static void main(String[] args) {

            System.out.println("********コンストラクタ 引数なし オブジェクト**************");
            // コンストラクタ 引数なし
            ChlidSampleClass csA = new ChlidSampleClass();
            // 親クラスのpublicメソッド
            csA.pubMethod();
            // 子クラスのメソッド
            csA.pubmethod();
            // 親クラスのprotectedメソッド
            csA.protectedMthod();

            System.out.println("********コンストラクタ 引数あり オブジェクト**************");
            // コンストラクタ 引数あり
            ChlidSampleClass csB = new ChlidSampleClass("AB88");
            // 親クラスのpublicメソッド
            csB.pubMethod();
            // 子クラスのメソッド
            csB.pubmethod();
            // 親クラスのprotectedメソッド
            csB.protectedMthod();

      }

}

実行結果
********コンストラクタ 引数なし オブジェクト**************
Parent class 引数なし
Child Class 引数なし
parentc class public method is called
chlid pubmethod is called
parentc class protected method is called
********コンストラクタ 引数あり オブジェクト**************
Parent class 引数なし
Child Class 引数あり
parentc class public method is called
chlid pubmethod is called
parentc class protected method is called

Java

Posted by arkgame