If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?
-
12-11-2019 - |
문제
I have 3 classes:
public class Alpha {
public Number number;
}
public class Beta extends Alpha {
public String number;
}
public class Gama extends Beta {
public int number;
}
Why does the following code compile? And, why does the test pass without any runtime errors?
@Test
public void test() {
final Beta a = new Gama();
a.number = "its a string";
((Alpha) a).number = 13;
((Gama) a).number = 42;
assertEquals("its a string", a.number);
assertEquals(13, ((Alpha) a).number);
assertEquals(42, ((Gama) a).number);
}
올바른 솔루션이 없습니다
제휴하지 않습니다 StackOverflow