aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I'm so confused, pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "I Watch "I New topic
Author

I'm so confused, pls help

Jim Yeung
Greenhorn

Joined: Nov 03, 2006
Posts: 5
Hi, all,
I got a question as follows:
=================================
class x{
y b=new y();
x(){
Sytem.out.print("x");
}
}

class y{
y(){
System.out.print("y");
}
}

public class z extends x{
y y0=new y();
z(){
System.out.print("z");
}
public static void main(String[] args){
new z();
}
}

The result is yxyz, I'm very confused, as my understanding, it should be yxz, how come? pls help me, thanks a lot!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

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??

Regards.
Jothi Shankar Kumar. S
[ November 03, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]

SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Jim Yeung
Greenhorn

Joined: Nov 03, 2006
Posts: 5
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??

Regards.
Jothi Shankar Kumar. S

[ November 03, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]



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 ?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

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.

Regards,
Jothi Shankar Kumar. S

[ November 04, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
[ November 04, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Jim Yeung
Greenhorn

Joined: Nov 03, 2006
Posts: 5
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.

Regards,
Jothi Shankar Kumar. S

[ November 04, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]

[ November 04, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]


Thanks a lot, Jothi, what confused me is y b=new y() is not within x's constructor, why does the code execute it before execute x's constructor?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"crazy_geek",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with display names get deleted, often without warning

thanks,
Dave
Jim Yeung
Greenhorn

Joined: Nov 03, 2006
Posts: 5
Originally posted by David O'Meara:
"crazy_geek",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with display names get deleted, often without warning

thanks,
Dave


Hi, David,
I've changed my display name, but my topic still shows "crazy_geek", not worked, was I doing sth wrong?
:roll:
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Hi Above,
Jothi answer are more than enough to gulp the basic concepts.

Thanks jothi


~Sanjeev Singh<br />SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I'm so confused, pls help
 
Similar Threads
k&b master exam :polymorphism doubt
Confused about this output-Constructor doubt
Object Construction Flow
Good Q's from JWhiz
get confusion in constructor execution