Программисты JAVA общаются здесь
Anonymous
Проблема с преобразованием в Java?
Сообщение
Anonymous » 05 окт 2024, 01:54
Кто-нибудь может объяснить, почему это происходит:
Код: Выделить всё
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();
}
Но результат:
Почему метод .setType() выполняет метод подкласса, а не метод суперкласса, хотя я выполняю повышающее приведение, как видно?
Подробнее здесь:
https://stackoverflow.com/questions/672 ... ng-in-java
1728082459
Anonymous
Кто-нибудь может объяснить, почему это происходит: [code]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(); } [/code] Но результат: [code]"inside red-apple class” [/code] Почему метод .setType() выполняет метод подкласса, а не метод суперкласса, хотя я выполняю повышающее приведение, как видно? Подробнее здесь: [url]https://stackoverflow.com/questions/6729039/problem-in-upcasting-in-java[/url]