Originally posted by Cameron Park:
Hi, I have a question about super(). Does it gets called implicitly always? For instance,
class ASubClass extends SuperClass{
ASubClass(String args){
//blah blah...
}
}
Was super() called in ASubClass(String)?
Hi Cameron,
The answer is not always. The only time super() is implicitly called is when subclass has a default constructor (i.e. the one without argument). The default constructor is either provided by the programmer or 'implicitly' inserted by the compiler. Note that the compiler only does that if you do not provide any overloading constructor in the class.
Please note the last statement of the above paragraph because it could cause problem for you. Here is how:
class A() {
A(int a) {
}
}
class B extends A {
}
The above subclass is equivalent to:
class B extends A {
B() { super(); } // this is done by the compiler
}
Since A has an overloading constructor, there is no default constructor. However B does not have any constructor, therefore
java compiler will provide one. That will implicitly call super(). But as you can see, there is no default constructor in A -> hence you will encounter error.
Hope that helps,
Lam
[This message has been edited by Lam Thai (edited April 20, 2001).]