aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Flow of the program in Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Flow of the program in Constructors" Watch "Flow of the program in Constructors" New topic
Author

Flow of the program in Constructors

swapna mallipudi
Greenhorn

Joined: Sep 10, 2009
Posts: 11
Can someone explain the flow on this program?
What is the output of the program?

Answer is devil boast devil devil boast

This question is from CertPal-Testing engine.
Thank you.
Shinelin Samuel
Ranch Hand

Joined: Mar 01, 2010
Posts: 55
Swapna,
When you instantiate a SubClass it instantiates the BaseClass as
Here is the flow,

Toast eye=new Toast(); - > Calls the constructor of the BaseClass Devil & Boast results devil boast
new Devil(eye).honey(eye); -> Calls the constructor of BaseClass Devil results devil
new Boast(eye).honey(eye); -> Call the BaseClass and SubClass Boast results devil boast


Regards,
Shinelin
Rodrigo Tassini
Greenhorn

Joined: Dec 29, 2008
Posts: 21
The first thing a constructor do, is a call to super().
swapna mallipudi
Greenhorn

Joined: Sep 10, 2009
Posts: 11
Hi Shinelin

Toast eye=new Toast(); - > Calls the constructor of the BaseClass Devil & Boast results devil boast ( I got this)


new Devil(eye).honey(eye); -> Calls the constructor of BaseClass Devil results devil (But here when new Devil(eye) constructor is called, The constructor in Line31 will be called right, why does the constructor in Line30 is also called. I am not able to understand that part)

new Boast(eye).honey(eye); -> Call the BaseClass and SubClass Boast results devil boast

Thank you.
Shinelin Samuel
Ranch Hand

Joined: Mar 01, 2010
Posts: 55
Swapna,

Sorry about the confusion,,

Here is how it works, i did not notice there is default and argumented construtors used.

Toast eye=new Toast(); - > Calls the constructor of the BaseClass Devil & Boast results devil boast
new Devil(eye).honey(eye); -> Calls the argumented constructor at line 31 of BaseClass Devil and results nothing
new Boast(eye).honey(eye); -> Call the BaseClass default constructor at line 30 and argumented constructor of Boast results devil
new Toast().honey(eye); -> Call the deafult constructor of Devil at Line 30 and Boast at line 18, results devil boast

finally you get --> devil boast devil devil boast
Rodrigo Tassini
Greenhorn

Joined: Dec 29, 2008
Posts: 21
Look for this code.


All constructor has a implicite call for a super().

This line

called a override constructor

then, a call to a super(), without argument, hapened.
swapna mallipudi
Greenhorn

Joined: Sep 10, 2009
Posts: 11
Thank you Shinelin & Rodrigo. It was clear now. I was thinking, when constructor with arguments is called(Boast(eye)) its super argument constructor is called so I didn't get the clear. A default constructor is called whenever a constructor with arguments is called.
Rodrigo Tassini
Greenhorn

Joined: Dec 29, 2008
Posts: 21
The first thing that happen into a constructor is a implicite call to a super(), but you can insert a call to a overriden constructor whith super(something).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flow of the program in Constructors