If I have this code:
interface Tre{
public void f();
}
public class TreA implements Tre{
int x = 1;
public void f(){
x++;
System.out.println("Y"+x);
}
public static void main(String[] a){
Tre a3 = new TreB(); a3.f();
}
}
class TreB extends TreA{
public void f(){
x--;
super.f();
System.out.println("X"+x);}
}
I do not understand why the output is Y1 X1
. If I extend the class TreA
, I'll overwrite its f()
method and get a copy of int x=1
. Calling super.f()
should call f()
belonging to the class TreA
but what about x
? I have never instantiated TreA
. Why does it use the x
belonging to TreB
? What about the scope in this case?