• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Anand Paralkar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And welcome to the Ranch
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:And welcome to the Ranch

    Indeed. Welcome Anand.

    Winston
     
    Anand Paralkar
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Anand Paralkar
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ....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
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Anand Paralkar wrote:A big thanks to both of you.

    You're most welcome. Hope it helps.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
    Is there a this page in HFJ? I can't remember
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!