i have read that until the super constructor has completed the subclasses instance variables cant get initialised.....my question is when do the variables get loaded? why are thier value 0 0 when the flow of the program has not even reached whre the instance variables are declared?
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Hi, That is not my problem. I got the idea, That is :
The order of execution is:
1. Set fields to default initial values (0, false, null)
2. Call the constructor for the object (but don't execute the body of the constructor yet)
3. Invoke the constructor of the superclass
4. Initialize fields using initializers and initialization blocks
5. Execute the body of the constructor
But my problem here is, How a Sub class method is called in the Super class Constructor? If I delete the method in the Super class, It give a Compilation Error!
After the super constructor has been run and before the first statement after super() or this(); The reason of the 0, 0 output is because the variables haven't been initialized because the super constructor has not completed yet.
It's fine. Take a Super class called SuperX and two Sub classes called SubX and SubY and those three class have their own method printVariable() (Overriding). Both of these Sub classes extend the Super class SuperX, then in the SubX class if we create an instance of that class, and in SubX constructor we called SuperX constructor. In SuperX constructor, we called the method printVariable, so the method in the SubX is invoked, since we are creating an instance of class SubX.