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

Good Q's from JWhiz

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at the following Q's. I fail to understand why "Y" is printing twice, can some expert give me "concept" behind it ?
Output is = YXYZ :confused
class X
{
Y b = new Y();
X()
{
System.out.print("X");
}
}
class Y
{
Y()
{
System.out.print("Y");
}
}
public class Z extends X
{
Y y = new Y();
Z()
{
System.out.print("Z");
}
public static void main(String[] args)
{
new Z();
}
}
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the flow of the program:
class X
{
Y b = new Y();//2
X()
{
System.out.print("X"); //4
}
}
class Y
{
Y()
{
System.out.print("Y"); //3 & 6
}
}
public class Z extends X
{
Y y = new Y(); //5
Z()
{
System.out.print("Z"); //7
}
public static void main(String[] args)
{
new Z(); // 1 calls z class
}
}

Y is printed twice because z class & its super class x instantiate y class.

Hope this helped

Originally posted by Wasim Ahmed:
look at the following Q's. I fail to understand why "Y" is printing twice, can some expert give me "concept" behind it ?
Output is = YXYZ :confused
class X
{
Y b = new Y();
X()
{
System.out.print("X");
}
}
class Y
{
Y()
{
System.out.print("Y");
}
}
public class Z extends X
{
Y y = new Y();
Z()
{
System.out.print("Z");
}
public static void main(String[] args)
{
new Z();
}
}


 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object is created, followings will occur in order:
1. memory is allocated for all its fields which are set
to default values (0 for numeric types, false for boolean,
'\u0000' for char and null for object reference)
2. invoke a superclass' constructor
3. initialize the fields using their initializers and initialization blocks
4. Execute the body of the constructor.
So, the body of constructor is executed last.
Before that, superclass's constructor is invoked using the
same steps 2-4. (This applies recursively until the constructor
for Object is reached.)
After returning from step 2, you proceed with step 3 & 4.
Hope this helps.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the execution order, here the instance variable and constryctor of the super class will be called first, hence
Y b = new Y();
will print "Y"
then constructor of the super class is called, hence
X()
{
System.out.print("X");
}
will print "X"
then the instance variables of the sub class are called , so
Y b = new Y(); in Z class will print "Y", and in the end construcor Z() is called within main().

Please correct me if I am wrong
--Farooq
 
Wasim Ahmed
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roopa, Nain and Farooq for explaination.
I still have a confusion about -- when sub class constructor calls super class constructor- the instance varibles of super class get intialized. Is this a true statement or I am missing something here? AND in above code Y is not the instance variable of class X then why it is getting initiated?
Thanks in advance.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Wasim Ahmed:
Nain Hwu's explanation is correct;
The order of the program :
First you call "new Z() in Main method"
1.variable y of Z class is set to null;
Next you call class X contructor .Before class X contructor
executes, variable b in class X is initialized -> That's why Y is
printed first.
Then class X contructor executes -> X is printed
Hope this help
------------------
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic