Campos ocultos aunque herencia
En el siguiente código de ejemplo:
class Parent {
int x =5;
public Integer aMethod(){
System.out.print("Parent.aMthod ");
return x;
}
}
class Child extends Parent {
int x =6;
public Integer aMethod(){
System.out.print("Child.aMthod ");
return x;
}
}
class ZiggyTest2{
public static void main(String[] args){
Parent p = new Child();
Child c = new Child();
System.out.println(p.x + " " + c.x);
System.out.println(p.aMethod() + " \n");
System.out.println(c.aMethod() + " \n");
}
}
Y la salida:
5 6
Child.aMthod 6
Child.aMthod 6
Por quep.aMethod()
no imprime 6 cuando p.x imprime 6?
Gracia
EditaOops un pequeño error tipográfico: La pregunta debería ser "¿por qué p.aMethod () no imprime 5 cuando p.x imprime 5"? Gracias @ thinksteep