• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

I'm so confused, pls help

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jim Yeung
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,
Jothi answer are more than enough to gulp the basic concepts.

Thanks jothi
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic