public class Toast extends Boast
{
Integer eye=1;
public static void main(String... args)
{
Toast eye = new Toast();
new Devil(eye).honey(eye);
new Boast(eye).honey(eye);
new Toast().honey(eye);
}
public void honey(Toast eye)
{
eye.eye = new Integer(-2);
}
}
class Boast extends Devil
{
public Boast(){System.out.print("boast ");}
public Boast(Toast eye)
{
eye.eye = new Integer(5);
}
public void honey(Toast eye)
{
eye.eye = new Integer(2);
}
}
class Devil
{
public Devil(){System.out.print("devil ");}
public Devil(Toast eye)
{
eye.eye = new Integer(1);
}
public void honey(Toast eye)
{
eye.eye = new Integer(9);
}
}
Choose: 1
Can you please use code tags and NOT quote tags when writing a java program.
Also the best way to learn about what the output should be is to run the program in "Debug" mode and follow the execution.
i understood it...first when devil(eye is called)its goes to Devil no args constructor...then while coming down it goes to Boast no args constructor.....again on next line since boast with args constructor is called it will go to Devil no args constructor by default...and print devil again...again when toast is called it will call its superclass constructors in order of heirarchy...i hope i am right....
Toast eye = new Toast();
new Devil(eye).honey(eye);
new Boast(eye).honey(eye);
new Toast().honey(eye); Above three lines produces the output.
Toast eye = new Toast(); Toast default constructor calls Boast no-arg constructor. Boast constructor calls Devil's no-arg constructor. No parent class to Devil so its no-arg constructor prints "devil" then Boast no-arg constructor prints "boast".
same exlanation : new Devil(eye).honey(eye); doe not print any thing.
new Boast(eye).honey(eye); prints "devil"
new Toast().honey(eye); prints "devil", "boast".....hope this helps
new Toast(); <- this will produce the output: devil boast
Toast will have a default constructor: public Toast(){} -> this will call the constructor of Boast() and constructor of Devil();
new Devil(eye).honey(eye); <- doesn't produce anything
new Boast(eye).honey(eye); <- Boast(Toast eye){} constructor will call Devil() and produce: devil new Toast().honey(eye);<- this will produce the output: devil boast
SCJP 6
Don't play dumb with me! But you can try this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth