This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
Joined: Jul 20, 2006
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().
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.
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.