A1 a1 = new A1("string passed"); // in subclass. will the above code 'always' invoke the superclass's default constructor even if default constructor in superclass is explicitly coded or not ? I just need the answer to clear my doubts , or for any reason it does not. I am trying executing.
hi, it will invoke the superclass constructor which take the string argument, and not the default constructor. this is called chaining of constructor. hope this helps ALkesh
Sorry, I can't agree with the first reply. It should be the default constructor of supclass to be called implicitly by every constructor of the derived class. If the default constructor not exist in super class, there will be compile error. Try the following code: class Base { Base(int i) {} } class Derive extends Base { Derive (int i){} } there will be compile error: No constructor matching Base() found in class Base. You can correct the code by delet the Base(int i){} in class Base, so the compiler will make a default constructor for Base. If there is anything wrong in my statement, please feel free to correct me. Thanks
Just add one point to my previous reply: If you called super(param list) in the constructor of derived class explicitly, it will not call super class's default constructor any more. The following code compile and run correctly too: class Base { Base(int i){} } class Derived extends Base { Derived(int i){ super(i); //some other valid code } }