wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor doubt" Watch "Constructor doubt" New topic
Author

Constructor doubt

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477


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
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: 477
Thanks again wise.I got it.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
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
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: 477



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: 477
I got it by myself through lots of different types of execution.Thanks.
kishore kovil
Greenhorn

Joined: Dec 21, 2006
Posts: 20
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
Good question !!
ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
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
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
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..

Please inform if iam wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor doubt
 
Similar Threads
Constructor doubt??
Code output -Doubt.
clear my doubt
Doubt in InnerClass
Constructor,why displaying null on this program