質問の言い方が正しいかどうかは正確にはわかりませんが、これらのコード行と混同しています。
public class First {
public String name() {
return "First";
}
}
public class Second extends First {
public void whoRules() {
System.out.print(super.name() + " rules");
System.out.println(" but " + name() + " is even better");
}
public String name() {
return "Second";
}
}
public class Third extends Second {
public String name() {
return "Third";
}
}
Second varSecond = new Second();
Third varThird = new Third();
varSecond.whoRules();
varThird.whoRules();
上記を実行すると、出力されます
First rules but second is even better
First rules but third is even better
なぜそうではないでしょうか:
First rules but second is even better
Second rules but third is even better
サブクラスを別のクラスのスーパークラスにすることはできますか? または、1つ(スーパークラス)しか存在できませんか?(上記のコード例を使用)First
が のスーパークラスであることは理解していますSecond
がSecond
、 のスーパークラスThird
ですか? それともFirst
スーパークラスThird
ですか?
互いに拡張する 10 個のクラスがある場合 (2 番目が最初に拡張し、3 番目が 2 番目に拡張するなど)、これらすべてのクラスのスーパークラスはFirst
?)