問題一覧に戻る
中級オブジェクト指向
問題62: superキーワード

superキーワードは親クラスを参照します。super()は親クラスのコンストラクタを呼び出し、使用する場合はサブクラスのコンストラクタの最初の文である必要があります。

class Animal {
String name;

Animal(String name) {
this.name = name;
}
}

class Cat extends Animal {
int age;

Cat(String name, int age) {
// // 親クラスのコンストラクタを呼ぶ
(name);
this.age = age;
}

void display() {
System.out.println(name + ", " + age);
}
}