This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flow of the program in Constructors

 
swapna mallipudi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing a constructor do, is a call to super().
 
swapna mallipudi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic