Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

this() call in a constructor

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

Hello my lovelies!

I've got another (though one) for you. I have this piece of code (still testing):



So basally I am creating an anonymous object with an 'x ' parameter. That should invoke House(String name){...} constructor. Now that one invokes this() which if I get it right would trigger no-argument constructor of House class. So... according to my calculations I should get output: h hn x but I get b h hn x
Now is it because Building no argument constructor prints b and as House extends Building , Building's no argument constructor is going to be called as well? Or is there some higher Maths to it?
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot to take into account that if no call to either this(...) or super(...) is issued, the compiler will automatically add in a no-arg super() call.

Does this answer your question?
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamila Bertran wrote:House extends Building , Building's no argument constructor is going to be called as well?

Yes, have look at this
outputChain.png
[Thumbnail for outputChain.png]
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think what would be the output, If we remove this() on line no 11? here
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, thanks guys. It does answer my question and clear my doubts. Ta!
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:What do you think what would be the output, If we remove this() on line no 11? here



Hmm... Following the logic the output should be b hn x
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamila Bertran wrote:Hmm... Following the logic the output should be b hn x

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic