5 Replies Latest reply: May 4, 2009 4:48 PM by 796447 RSS

    Why super constructor call exist?

    843789
      hi.

      The super constructor is always called from the subclass (implicitly or explicitly). My question is: why?
      I found one reason to this: I can call the super class' overrided methods using the super keyword.

      Does someone knows other reasons?
      What is the main purpose of this functionality?
        • 1. Re: Why super constructor call exist?
          843789
          tivrfoa wrote:
          hi.

          The super constructor is always called from the subclass (implicitly or explicitly). My question is: why?
          I found one reason to this: I can call the super class' overrided methods using the super keyword.
          Huh?
          • 2. Re: Why super constructor call exist?
            843789
            Hello tivrfoa,

            it's the only way to initialize the (including but not limited to private) state of the super class fields without knowing the internal workings of the implementation. Also, if you were to initialize the super class state, it would break capsulation; you could not make changes to a class without potentially breaking subclasses.

            With kind regards
            Ben
            • 3. Re: Why super constructor call exist?
              843789
              tivrfoa wrote:
              hi.

              The super constructor is always called from the subclass (implicitly or explicitly). My question is: why?
              I found one reason to this: I can call the super class' overrided methods using the super keyword.

              Does someone knows other reasons?
              What is the main purpose of this functionality?
              You have to call the "super" constructor if you want really "super" (i.e.: terrific) results.

              Seriously, try googling for this; I came up with this, which I only looked at briefly.
              • 4. Re: Why super constructor call exist?
                843789
                I would start here: [http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html]
                • 5. Re: Why super constructor call exist?
                  796447
                  tivrfoa wrote:
                  hi.

                  The super constructor is always called from the subclass (implicitly or explicitly). My question is: why?
                  I found one reason to this: I can call the super class' overrided methods using the super keyword.

                  Does someone knows other reasons?
                  What is the main purpose of this functionality?
                  Let's suppose you have a base class which contains 5 private fields. Somewhere a constructor in that class initializes those 5 fields.

                  Now let's say you created a subclass of the above class, and added 2 more fields. Your constructor in this class cannot directly initialize any of the base class fields (remember, they are private).

                  If your subclass constructor never invoked a base class constructor, how would you expect those other 5 fields that you inherited, to be initialized -- by magic?