4 Replies Latest reply on Jul 14, 2012 6:03 PM by 949148

    Constructor Chaining

      I understand the need for chaining constructors at its present state. However, the way that it is handled at mid-level in a chain seems inadequate and/or inefficient especially when you are overloading and you have lots of classes at all levels of the chain.

      For example:
      abstract class Grandparent {
          private int instVar;
          public Grandparent() {}
          public Grandparent(int i) {
              instVar = i;
          public string getInstVar() {
              return instVar;
      public class Parent extends Grandparent {
          public Parent() {}
          public Parent(int i) {
      public class Child extends Parent {
          public Child() {}
          public Child(int i) {
              // additional code
      In this case my issue is with the Parent class (if there are many of them). I realize there might be plug-ins for your IDE to automatically write this bit for you (of which I do not have). Considering that the compiler will create a blank constructor for you if you do not write one yourself, shouldn't it allow for all possibilities, especially if you have overloads (possibly many different ones) in your Grandparent that only get any use in the Child (in this case).

      I guess in my mind, I think a constructor should logically be something like this:
      public Constructor ([]??()) {
      I only wrote the syntax this way as an example. The [] allows any possibility to flow through the Parent constructor up to the Grandparent without expressly declaring any specifics as such. If it is null (or no parameters exist), it defaults to the no parameter constructor residing with the Grandparent. Essentially acting as a placeholder. There would have to be rules for such a thing, that is a given, but I believe it allows the flexibility and efficiency for programmers to not have to worry about all the overloaded constructors until it hits the concrete class, so-to-speak, if you have anything to add at all.

      Maybe I making a big deal out of nothing, or maybe I am making no sense at all, but its just a thought. Cheers!

      Edited by: Initiated on Jul 12, 2012 2:44 PM
        • 1. Re: Constructor Chaining
          The compiler cannot possibly know (a) which base class constructor you want it to call, (b) what arguments you want to supply to it, (c) what formal arguments you want derived classes to provide, and (d) what to do with them when received.
          • 2. Re: Constructor Chaining
            It isn't "nothing" you're making a big deal out of, but it isn't very common to have a deep inheritance tree and also a lot of overloaded constructors. Sure, there are places where it happens (e.g. Swing GUI components) but in most designs you won't encounter that sort of thing.
            • 3. Re: Constructor Chaining
              EJP: Thank you for your response. I will think about this more as an exercise (in futility?) to better myself and this particular idea. You've definitely given me something to expand upon when I get bored.

              DrClap: I think about these things when working on back-burner projects such as compliance applications dealing with HL7 and its library.
              • 4. Re: Constructor Chaining

                Edited by: Initiated on Jul 14, 2012 11:03 AM