• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor doubt

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again wise.I got it.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it by myself through lots of different types of execution.Thanks.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question !!
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic