コンストラクタの注意点
またまた基本的な事メモ汗
なるので要注意しないといけない。
class SuperA{
SuperA(String str){System.out.println("Aコンストラクタ");}
}
class B extends SuperA{
B(){System.out.println("Bコンストラクタ");}
}
public class ConstTest {
public static void main(String args) {
B b = new B();
}
}
コンストラクタが実行されるんですが、スーパークラスに引数ありの
コンストラクタを定義していた場合には下記のどちらかを明示的に
記載しないとコンパイルエラーになってしまいます。
class SuperA{
SuperA(){}
SuperA(String str){
System.out.println(str);
}
}
class B extends SuperA{
B(){
super("Aコンストラクタ");
System.out.println("Bコンストラクタ"); }
}
public class ConstTest {
public static void main(String args) {
B b = new B();
}
}
要注意です。