4 Replies Latest reply: May 11, 2011 2:45 PM by 796440 RSS

    invoking super() in a subclass contructor

    860579
      please consider:
      public MyInputStream extends InputStream {
          MyInputStream(String name) {
            super(); // <--- unnecessary
            .....
          }
      }
      Invoking a parent's default constructor from within a subclass's constructor does not seem necessary, right?

      A default constructor does not initialize the state of an object?
      It does only two things:
      (1) loads the class's methods, if not already loaded.
      (2) allocates memory for the object // <-- unless "OutOfMemoryErrors" are an issue, this is transparent

      In summary:
      This all explains why subclassable and serializable classes must have defailt constructors, right?
        • 1. Re: invoking super() in a subclass contructor
          sabre150
          azjp wrote:

          Invoking a parent's default constructor from within a subclass's constructor does not seem necessary, right?
          It is not necessary if you don't explicitly invoke a non-default constructor since it will then invoke the default constructor for you.

          >
          A default constructor does not initialize the state of an object?
          Yes it does.
          It does only two things:
          (1) loads the class's methods, if not already loaded.
          No - the class loader does this.
          (2) allocates memory for the object // <-- unless "OutOfMemoryErrors" are an issue, this is transparent
          The constructor does not do this. The JVM does this.

          >
          In summary:
          This all explains why subclassable and serializable classes must have defailt constructors, right?
          I don't see how you make this a consequence of your other nonsense.

          P.S. What is 'subclassable' ? It's not an interface in JDK1.6 or earlier.
          P.P.S. Have I just answered some of your homework questions?
          • 2. Re: invoking super() in a subclass contructor
            802316
            azjp wrote:
            (1) loads the class's methods, if not already loaded.
            The classes methods are loaded in stages, first when the class file is accessed, next when it is invoked.
            (2) allocates memory for the object // <-- unless "OutOfMemoryErrors" are an issue, this is transparent
            memory for an object is allocated as seperate byte code instruction before a constructor is called.
            This all explains why subclassable and serializable classes must have defailt constructors, right?
            Serializable class requires a default constructor because it makes it easier for the library to know which constructor to call with what arguments to re-create the object. i.e. the default constructor with no arguments.

            Newer serialisation libraries avoid the issue by creating the object without calling a constructor and setting all the fields as required.
            • 3. Re: invoking super() in a subclass contructor
              860579
              sabre150 wrote:

              P.S. What is 'subclassable' ? It's not an interface in JDK1.6 or earlier.
              I think "subclassable" means a non-final class?
              Or, a class with a constructor that invokes an overridable method should probably be treated as not being subclassable.
              I really don't know. This is just my second post.
              P.P.S. Have I just answered some of your homework questions?
              no. I just noticed that invoking super(); in my constructors did not "seem" necessary. I am a newbie, so I wanted to know exactly what is going on.

              thanks for the answer.
              • 4. Re: invoking super() in a subclass contructor
                796440
                azjp wrote:
                sabre150 wrote:

                P.S. What is 'subclassable' ? It's not an interface in JDK1.6 or earlier.
                I think "subclassable" means a non-final class?
                Why are you asking? It's your word, certainly not standard Java nomenclature.
                no. I just noticed that invoking super(); in my constructors did not "seem" necessary.
                Correct. Explicitly invoking the no-arg super() is never necessary, but some development shops might adopt the convention for clarity--that is, you always name exactly the superclass c'tor you're invoking.