誰かがこれが起こっている理由を説明できますか?
class Apple {
String type;
setType(){
System.out.println("inside apple class");
this.type = "apple";
}
}
class RedApple extends Apple {
String type;
setType() {
System.out.println("inside red-apple class");
this.type = "redapple";
}
}
int main {
RedApple red = new RedApple();
Apple apple = (Apple) red;
apple.setType();
}
ただし、生成される出力は次のとおりです。
"inside red-apple class”
ご覧のとおり、アップキャストしているのに、なぜ.setType()
メソッドはスーパークラスメソッドではなくサブクラスメソッドを実行するのですか?