wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Overloaded constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloaded constructor question" Watch "Overloaded constructor question" New topic
Author

Overloaded constructor question

shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76


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

Joined: Nov 23, 2010
Posts: 4
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14195
    
  20

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?

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39089
        
      23
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39089
        
      23
    Jesper, you missed out the bit where the no-arguments constructor in Building calls the no-arguments constructor in Object.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39089
        
      23
    . . . and welcome to the Ranch
    shuba gopal
    Ranch Hand

    Joined: May 12, 2011
    Posts: 76
    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
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14195
        
      20

    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).
     
    GeeCON Prague 2014
     
    subject: Overloaded constructor question