(added tags and clarified topic title) [ July 21, 2006: Message edited by: Barry Gaunt ]
Neelesh Bodas
Ranch Hand
Joined: Jul 20, 2006
Posts: 107
posted
0
The instance variable 's' in the child is initialized to "Child" after the superclass constructor runs. However, they are zero-initialized (i.e. references initialized to null) before the superclass constructor runs.
This explains why output shouldnot be "Child".
nitin pokhriyal
Ranch Hand
Joined: May 19, 2005
Posts: 263
posted
0
how do i forget this?
thanks buddy for your help.
regards, nitin
ram gaurav
Ranch Hand
Joined: Mar 29, 2006
Posts: 208
posted
0
Sir i have one confusion , like when in Parent constructor mathord() is called , then according to me it should be called of Parent but its calling of Child , can you please explain why this is happening.
Thanks Gaurav
Neelesh Bodas
Ranch Hand
Joined: Jul 20, 2006
Posts: 107
posted
0
instance methods are dynamically bound. In other words, the exact method which is dispatched depends on the actual object referenced by "this" reference. Note that constructors are no exception to this, and the methods referenced from the constructors are also dynamically bound. Thus, when the Parent() constructor is called through Child() constructor, the method() call, though done from the Parent constructor, is resolved to Child.method() and not Parent.method().
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
Originally posted by ram gaurav: Sir i have one confusion , like when in Parent constructor mathord() is called , then according to me it should be called of Parent but its calling of Child , can you please explain why this is happening.
Thanks Gaurav
Check out what overriding means and understand what happens when you call an overridden method in a base class. The call method() in the base class is the same as this.method() where this is a reference to the base class object.
1) Call the constructor 2) Call Super constructor if any(here parent) 3) Initilize static & instance variables(S = "parent") 4) Execute the statements in the constructor(call to method... This referees the method in the child. Not the method in the Parent.)
So when the method in the child is called the variable "S" is yet to be initilized. So "null" is printed.
If u make the the string in child as static, then u can have "child" O/p.
Cos static will be loaded at the time of loading the class.
Since the method is overridden in the child class the overridden method will take the higher priority.
Correct me if i'm wrong. [URL=Order for Object Creation] [ July 21, 2006: Message edited by: Barry Gaunt ]
Douglas Chorpita
Ranch Hand
Joined: May 09, 2006
Posts: 97
posted
0
Yes. You are right, Ganeshkumar.
Static initializations (and initializer blocks) are processed before the main() method is called. So, of course, the String would be already initialized.