• 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
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Overloaded constructor question

 
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
     
    Marshal
    Posts: 75857
    361
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 75857
    361
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 75857
    361
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    . . . and welcome to the Ranch
     
    shuba gopal
    Ranch Hand
    Posts: 87
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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).
     
    reply
      Bookmark Topic Watch Topic
    • New Topic