This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Mu understanding about this problem,when we create new Child(),the control goes to execute the Child class constructor.over there,first statement is always super().So goes to execute Parent() constructor.Over there ,(Object constructor is called first)then we want method() method to execute,so go to the method and printing s value.For the overall execution upto this,we didn�t initialize s value.so s value is printing as null.Upto this ,I am very clear.
I changed the above program as below.
Still s=null value is printing instead of s=parent.What part am I missing here to get? I think,String s is overidded in both Parent and Child.But I am not sure,how this overrided variable works here.Please help me.
wise owen
Ranch Hand
Joined: Feb 02, 2006
Posts: 2023
posted
0
It is nothing to do with the constructor. The constructor in your parent class calls "method()" which has been overrided by it's child class (Child). The runtime binding will call the child class' "method()" and not the method in Parent class.
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
posted
0
Thanks again wise.I got it.
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
posted
0
When the subclass did not override method(),Parent class method() will execute from Parent() constructor.the coding is below.
But here s=null is not printing.Instead it prints s=Parent.But how.Please help me wise.
Aniket Patil
Ranch Hand
Joined: May 02, 2006
Posts: 218
posted
0
This is because, after a call to super() in the Parent constructor, the instance variable initializers and instance initializers will execute. This will cause assignment of "Parent" to the instance variable s. Only after this will the call to method() within the constructor execute.
This is the reason why Parent is printed at the console.
SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
posted
0
on the above program,i got a doubt again. when we execute Child() constructor at first,super() is executed then why intialiser of child instance s is not changed to s="Child" instead it is printing s=null on line 1.why i got suddenly this doubt means because of the Aniket reply.
This is because, after a call to super() in the Parent constructor, the instance variable initializers and instance initializers will execute. This will cause assignment of "Parent" to the instance variable s. Only after this will the call to method() within the constructor execute.
After the Parent constructor super(),the intializers are running means why that type of process is not going on Child constructor too.At what time exactly the Child s="Child" is intialized.Please help.
Shiva Mohan
Ranch Hand
Joined: Jan 05, 2006
Posts: 465
posted
0
I got it by myself through lots of different types of execution.Thanks.
kishore kovil
Greenhorn
Joined: Dec 21, 2006
Posts: 20
posted
0
Hi ShivaMohan, That was a very nice question indeed. The explanation is also good:
"This is because, after a call to super() in the Parent constructor, the instance variable initializers and instance initializers will execute. This will cause assignment of "Parent" to the instance variable s. Only after this will the call to method() within the constructor execute."
It means, only after completely executing initializers ,the control returns to sub class constructor
victor kamat
Ranch Hand
Joined: Jan 10, 2007
Posts: 247
posted
0
Good question !!
ram gaurav
Ranch Hand
Joined: Mar 29, 2006
Posts: 208
posted
0
Yes, the question asked by Shiva is really vaery god.
I have tried the question asked by Shiva and what i noticed that when we talk about Child Constructor firstly the full constructor is get completed and then initialization will take place but when we talk about Parent class its Vice - Versa.
I am also not able to digest this thing.
Can somebody please explain it bit clearly.
Thanks Gaurav
Priya Viswam
Ranch Hand
Joined: Dec 28, 2006
Posts: 81
posted
0
The order of execution will be as follows.
1. Base static block 2. Derived static block 3. Base instance block & instance variable initialization of base (in the order in which it appears in the code.) 4. Base Constructor 5. Derived instance block & instance variable initialization of derived (in the order in which it appears in the code.) 6. Derived Constructor
If there are more than one static block, then it will be executed in the order in which it comes in the code. Same is the case with instance block also.
See this example
The output will be
Base static block Derived static block Base instance block Base instance variable initialization Base Constructor Derived instance block Derived instance variable initializaition Derived Constructor
SCJP 1.5<br />SCWCD 1.4
siva prasaad
Greenhorn
Joined: Jan 10, 2007
Posts: 23
posted
0
Hi all,
In program given by Shiv Mohan,
The parent class constructor is calling an instance method.
I think instructors can call only Static methods not the instance methods..