• 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

Contructors

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone explain the output : b h hn x

The String arg constructor calls its superclass constructor and that explains the b h in the output. I cannot understand the hn. What about the no arg superclass constructor?

Can anyone just run me through the above example?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just follow the call path, house(string) calls house() calls building(); The "hn x" should be the easiest to follow as that is via the first call.

Henry
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 19 calls line 12. Line 14 calls line 11. There's an invisible call to "super" at the beginning of House's no-argument constructor, inserted by the compiler; that goes to line 3, which prints "b". Then control returns to line 11, which prints "h". Then control returns to line 15, which prints "hn x". Then control goes back to line 20, and the program exits. The "Building" constructor with a String argument is never used.
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

So in what condition will the Building String arg constructor will be invoked.
1. if there is a this(); defined in the no arg building constructor?

any other case?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanjana Sharma wrote:
So in what condition will the Building String arg constructor will be invoked.



Without changing the Building or House code? Only if somebody used 'new Building("x")' . Generally, you might want House(String) to call Building(String); you'd do that like

 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So two cases:
1. if we had

2.

or we simply called Building("x");

Correct me if i am wrong.
 
Ranch Hand
Posts: 87
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanjana Sharma wrote:

When you say this(); , you are invoking the no arguement constructor of the Building. So just think what will happen when you call the no argument contructor of the Building from the no argument contructor of the Building itself .

Why don't you try compiling it and see the result ...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanjana Sharma wrote:
Can anyone explain the output : b h hn x

The String arg constructor calls its superclass constructor and that explains the b h in the output. I cannot understand the hn. What about the no arg superclass constructor?

Can anyone just run me through the above example?



By writing a no argument constructor, you are forcing the child classes to add a constructor with super();
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it. thanks a lot..
 
prem pillai
Ranch Hand
Posts: 87
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanjana Sharma wrote:I got it. thanks a lot..



what? Tell us your current understanding ...
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I complied the code with different combinations.

if I try to compile : , i will be invoking the same constructor again for which complier throws a error.
 
prem pillai
Ranch Hand
Posts: 87
Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good .... So you have learned two major things from this thread.
1) Recursive invocation of a constructor is not allowed.
2) To try out things yourself before posting a query ..

Now can you think about why recurion is not allowed in constructors?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Line 19 calls line 12. Line 14 calls line 11. There's an invisible call to "super" at the beginning of House's no-argument constructor, inserted by the compiler; that goes to line 3, which prints "b". Then control returns to line 11, which prints "h". Then control returns to line 15, which prints "hn x". Then control goes back to line 20, and the program exits. The "Building" constructor with a String argument is never used.




Very correct said.I am fully agree with you.
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya very correctly explained.

Prem Sir, we cant have recursive constructor calls , cos constructor invocation marks the creation of an instance. so recursive calls dont make sense.

Correct me if i am wrong and i missed something.
 
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not "ya" or "cos", please.
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i apologize for the usage.
 
Campbell Ritchie
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies accepted
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So , was i right on the reasoning for recursive constructor invocation?
if not, correct me
 
Campbell Ritchie
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Varun Saini,
Your post was moved to a new topic.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic