PROGRAM 1
----------
-----------------------------------------------
PROGRAM 2
-----------------------------------------------
Flow of program 1
----------------------
When the object of class b is constructed
new B(); it calls the default constructor of class B which in-turn
calls superclass constructor but before the superclass constructor
executes it constructs an object of class C which calls default
constructor of class C printing
"C" The constructor call returns to constructor of class A and
"B" gets printed and then call returns back to constructor of class B where again b
"B" gets printed
So the output is
CBB Flow of program 2
----------------------
BUT IN CASE OF PROGRAM 2 the behaviour or i should say the flow seems to be bit different.
When the object of class Z is constructed...
First member varaible of class Z is initialized i.e... Y y = new Y(); So this leads to calling the constructor of class Y which in turn
prints Y on the screen Then the control return back to the constructor of class Z where an implicit call to the default constructor of superclass X gets called.
Now here the diffence is that in this case FIRST X IS PRINTED FROM THE CONSTRUCTOR OF CLASS X AND THEN THE CLASS MEMBER Y b = new Y(); gets executed So in program 1 first the class member is initialized and then the constructor is executed but in case of program 2 first the constuctor is executed and then the class level member is initialized ...... why
Why is that difference in execution....... pls help !!!
[ September 23, 2003: Message edited by: Manish Sachdev ]