The answer is quite obvious in your code. Didnt you see that you are calling the the class y constructor twice?? Hence the result YXYZ. See the comments in the code below.
Your code,
Try changing in the class z as below and tell me what the O/P will be?
public class z extends x{
z(){ System.out.print("z"); } y y0=new y(); public static void main(String[] args){ new z(); } }
What will be the O/P in tha above case?? YXZY. did you get it??
Regards. Jothi Shankar Kumar. S [ November 03, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Originally posted by Jothi Shankar Kumar Sankararaj: Hi Above,
The answer is quite obvious in your code. Didnt you see that you are calling the the class y constructor twice?? Hence the result YXYZ. See the comments in the code below.
Your code,
Try changing in the class z as below and tell me what the O/P will be?
public class z extends x{
z(){ System.out.print("z"); } y y0=new y(); public static void main(String[] args){ new z(); } }
What will be the O/P in tha above case?? YXZY. did you get it??
Hi, Jothi, thanks for your response, if you don't mind my stupidity, is it true the program has to first execute x's other objects located before x's constructor when calling x's constructor ?
In real life the rule is parent must exist before the child can. In OO terms this means parent class must exist before the child class can. So whenever you call the child class constructor, there is an implicit call to it's parent constructor. Never forget this.
The program runs sequentially, so any code in a contructor is taken care in the order in which they are written.
Originally posted by Jothi Shankar Kumar Sankararaj: Hi "I Dont know you name",
In real life the rule is parent must exist before the child can. In OO terms this means parent class must exist before the child class can. So whenever you call the child class constructor, there is an implicit call to it's parent constructor. Never forget this.
The program runs sequentially, so any code in a contructor is taken care in the order in which they are written.