Hi,
Please see the code below. If at *** variable "i" is not set to 4, the output will be 3, if it is set, it'll be 4.
My question is... well, I don't know exactly, there could be many. This whole thing is strange for me.
(For example it seems that the constructor for the super class runs when i access an instance variable from it.)
Could somebody explain this topic or maybe help me with directing to an article?
public class TryThis {
public static void main (String args[]){
Child c = new Child();
c.seeSuper();
}
}
public class Parent {
int i;
Parent(){
i = 3;
}
}
public class Child extends Parent{
Child(){
//i = 4; //***
}
public void seeSuper(){
System.out.println(super.i);
}
}
note: Parent could also simply look like this:
public class Parent {
int i = 3;
}