• Post Reply Bookmark Topic Watch Topic
  • New Topic

this() call in a constructor  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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?
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:Hmm... Following the logic the output should be b hn x
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!