wrt constructor chaining
Hi folks. The compiler provides a default constructor when we dont provide any. Let us say I provide a constructor with an argument and if I try to instantiate a subclass of that class then which constructor of the superclass gets called as it doesn't have a no-arg constructor and the compiler doesnt provide one in this case?
Have you done any tests to see what behaviour you can observe?
If you don't write any constructors, the compiler will provide you with a no-arg constructor (as you said). The implementation of this will just call super() - that is, the no-args constructor of the super class. If the super-class doesn't have a no-args constructor, then the compiler is unable to provide a default no-args constructor for this class, and will produce an error. So... if the super class doesn't have a no-args constructor then your sub-class must provide its own constructors.
On a related note, the first thing that any constructor does is to call a super constructor. If you don't do it yourself in the code, then the compiler will insert a call to super() for you. Again, if no such super constructor exists, you will get an error. So... further to what I said above, each of the constructors you provide in this case must delegate to an appropriate (non-default) constructor in the super class.
>
> On a related note, the first thing that any
> constructor does is to call a super constructor. If
> you don't do it yourself in the code, then the
> compiler will insert a call to super() for you.
> Again, if no such super constructor exists, you will
> get an error. So... further to what I said above,
> each of the constructors you provide in this case
> must delegate to an appropriate (non-default)
> constructor in the super class.
Danny,
Correct me if I am wrong.
1. Compiler will only provide default constructor. And only when no other parameterized or No-arg constructor is defined.
2. Any Contructor will not call super() unless you specify.
3. If you donot create parameterised constructors in a sub class, the same constructors in the super class get hidden (You will still inherit the properties but not the constructor)
#2 meaning.. during instantiation of a class, with no params, compiler will search for the nearest no-arg constructor up the inheritance.
Eg:
A -> B -> C
new C() will first try if there is C(). If any other C(x,...) is defined, and C() is not defined, it will throw a compilation error [C() is not defined] there itself, Default will not be provided by the compiler in this case.
If C() is not defined, the "same" check will be applied to B(), then to A()
sar.ha at 2007-7-14 21:13:17 >

> 2. Any Contructor will not call super() unless you
> specify.
No. It will always insert super() if the base class has a no-arg constructor (implicit or explicit).
> 3. If you donot create parameterised constructors in
> a sub class, the same constructors in the super class
> get hidden (You will still inherit the properties but
> not the constructor)
No, this is meaningless. Constructors are never inherited.
> #2 meaning.. during instantiation of a class, with no
> params, compiler will search for the nearest no-arg
> constructor up the inheritance.
No. This is all imaginary. If C() exists implicitly or explicitly it will be called. If it doesn't, a compilation error will be thrown. That's it. There is no search up the inheritance chain.
ejpa at 2007-7-14 21:13:17 >
