Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Flow of the program in Constructors

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic