• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloaded constructor question

 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi, the output for this code is "b h hn x". I expected the output to be "b bn h hn x". I understand that the superclass constructor will be invoked first. I expected that String constructor will also be invoked.
 
Stuart James
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This() calls the default constructor on the sub class.

You *probably* want to call the non-default super constructor instead - if I understand your question. Though this will not give you your expected output either.

This and Super calls cannot appear in the same constructor.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Let's see what's happening here.

  • In line 12 you create a new House with argument "x ". That will call the constructor in line 9.
  • The first thing that constructor does, by calling this();, is call the no-args constructor in line 8.
  • Since there is no explicit call to a superclass constructor (super(...);), first the no-args constructor of the superclass will be called. That's on line 2. That constructor prints: b
  • Then we continue on line 8. The constructor there prints h
  • Then we continue on line 10. There, hn x is printed.


  • So, in the end we have: b h hn x

    The constructor of Building which takes a String argument (lines 3 - 5) is not called anywhere. Why do you expect it to be called?
     
    Campbell Ritchie
    Marshal
    Posts: 52587
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stuart James wrote:This() calls the default constructor on the sub class. . . .
    Not quite. It calls the no-arguments constructor in the same class (House). Because you have defined a constructor, there is no default constructor. Since there is no explicit super(...) call in that no-arguments constructor, the JVM calls the no-arguments constructor in the superclass.

    And I see Jesper has beaten me to it.
     
    Campbell Ritchie
    Marshal
    Posts: 52587
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper, you missed out the bit where the no-arguments constructor in Building calls the no-arguments constructor in Object.
     
    Campbell Ritchie
    Marshal
    Posts: 52587
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    . . . and welcome to the Ranch
     
    shuba gopal
    Ranch Hand
    Posts: 76
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all...
    Jesper thank you for the detailed explanation. Since the superclass constructors are invoked first, I thought the superclass constructor with a string argument will be invoked when Horse is instantiated with a string argument. Now it sounds silly.

    Thanks for the welcome
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 15715
    73
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Jesper, you missed out the bit where the no-arguments constructor in Building calls the no-arguments constructor in Object.

    Yes, you're right Campbell, since all classes implicitly extend Object, the no-args constructor of Object is also called (although that's not visible in the code itself).
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!