• 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,why displaying null on this program

 
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 Child class loaded in main() method,at that time s=�Child� member variable is initialized.Right?
Then why the answer s=null printing when we access super class constructor method() will call Child class method() method?I thought it would print s=Child.Please Explain the rules.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the superclass is initialized during the process of initializing the subclass. The instance variables in the subclass are given default values, and before they are initialized there is an implicit call to the no-args constructor of the superclass. During that call, an overridden method is called. Since it is overridden in the subclass and that is the method that executes. Since the subclass has not finished initializing yet, the value of its instance variables are the default values.
[ September 20, 2006: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The simple & straight answer is, the instance variables are created and assigned default values until the super class constructor is executed, then the values are assigned.



Output:
--------------

Inside this Parent Hello
Inside Method Child null
Inside this Child Hello
Inside Method Child Hello


As you see, the Parent method() is not called....
 
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
Srinivas and Keith thank you so much for all your help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic