Forums Register Login

Why Would We Need to Call "super();" Explicitly?

+Pie Number of slices to send: Send
Hi,

This is my first post in this forum and I must thank the authors of this site for helping out beginners like me. Thank you!

Just began learning Java (with the book Head First Java). I read that we can invoke the constructor of a class's super-class by including the statement "super();". I also read that the compiler implicitly does that - it inserts an invocation to a class's' super class constructor.

Why then would anyone require to do it explicitly? Any particular situation where one would want to just invoke a class's super-class constructor?

Thanks once again,
Anand
+Pie Number of slices to send: Send
 

Anand Paralkar wrote:Why then would anyone require to do it explicitly?


Because the only call that the compiler will add for you is super() - ie, a call to the "default" constructor of the superclass - and it's perfectly possible that the superclass doesn't have one (it's not required to).

Personally, I prefer to put in all those supposedly "optional" things, like:
  • super()
  • The "default" (no-args) constructor.
  • extends Object
  • explicitly myself.

    And just FYI, 'super' is also used in other places to say that you specifically want a field/method of the superclass, not the current one (this).

    HIH

    Winston
    +Pie Number of slices to send: Send
    And welcome to the Ranch
    +Pie Number of slices to send: Send
     

    Campbell Ritchie wrote:And welcome to the Ranch


    Indeed. Welcome Anand.

    Winston
    +Pie Number of slices to send: Send
    Winston and Campbell,

    Thank you for the warm welcome. As far as Java goes, my situation is summarized in the words of Robert Frost - "And miles to go before I sleep, And miles to go before I sleep." !

    As for the call to "super();" goes, here is what I understand:

    1. It's optional to use "super();" in a class constructor, but as a matter of taste/practice, one may use it explicitly.
    2. One may want to include a with-argument super class constructor, in which case, a class may use "super(something, something, ...);". And when the with-argument super-class constructor is used, the no-argument constructor is not inserted by the compiler implicitly.

    Hope my understanding is correct.

    Thank you for looking into this.

    Regards,
    Anand
    +Pie Number of slices to send: Send
    There is more to this:-
  • If you write this(...); as the first line in a constructor, you call that particular overloaded constructor in the same object.
  • If you write super(...); as the first line in a constructor, you call that particular constructor in the supertype's object.
  • If you write super(); as the first line in a constructor, you call that no‑arguments constructor in the supertype's object.
  • The calls this(...) super() and super(...) must be the first thing in the constructor so you can only use one of them.
  • If you don't write any of them the constructor inserts the equivalent of super(); so you may miss out a super(); call.

  • You can only use a constructor which actually exists, otherwise the compiler will complain.
    If you want to use a particular constructor you must call it explicitly.
    More details (maybe hard to understand) in the Java Language Specification.
    +Pie Number of slices to send: Send
     

    A few minutes ago, I wrote: . . .
    If you want to use a particular constructor you must call it explicitly.
    . . .

    That means except for a no‑arguments supertype's constructor.
    +Pie Number of slices to send: Send
     

    Anand Paralkar wrote:in the words of Robert Frost - "And miles to go before I sleep, And miles to go before I sleep." !


    Good old RF; the master of melancholy. Hope you're not feeling too melancholic.

    Winston
    +Pie Number of slices to send: Send
    ....I am yet to get to the point of "this"! (Not yet come to "this" page in Head First Java. ) But I think I get it now. Strangely, I read the spec and understood most parts of it!!

    I am not exactly melancholic, but anxious definitely. I am learning Java so that I can deliver a live Java project that is sitting on my head right now. Ever heard anybody do that? I just hope the customer doesn't get his eyes on "this".

    A big thanks to both of you.

    Regards,
    Anand
    +Pie Number of slices to send: Send
     

    Anand Paralkar wrote:A big thanks to both of you.


    You're most welcome. Hope it helps.

    Winston
    +Pie Number of slices to send: Send
    You're welcome
    Is there a this page in HFJ? I can't remember
    What's brown and sticky? ... a stick. Or a tiny ad.
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 478 times.
    Similar Threads
    question from Applied Reasoning mock exam
    can we have two constructors being called for the same object ???
    Use of constructor in abstract class?
    Using the Keyword super
    Inner Classes Extension
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 02:13:29.