How do you know when to use super?

Hi newsgroup,When you inherit from a class, how do you knowwhether you must / could / mustn'tinvoke an explicit super() in the constructor of your class? Where do you get the knowledgeto decide that for a given superclass hierarchy?
[275 byte] By [Just_Me_Againa] at [2007-11-27 9:53:04]
# 1
You always have to call super in the child class constructor as the first statement. The only exception is when the parent class have an empty constructor.
jaxiana at 2007-7-13 0:22:16 > top of Java-index,Java Essentials,Java Programming...
# 2

> The only exception is when the parent class have an empty constructor.

If you do not supply a call to another constructor of the same class or a construtor of the parent class as the first statement of your constructor, an implicit call to the super class's default (no-arg) constructor is inserted by the compiler. Now if that implicitly invoked constructor does not exist or is not accessible, it is a compile-time error situation.

BIJ001a at 2007-7-13 0:22:16 > top of Java-index,Java Essentials,Java Programming...
# 3
Wich one is appropriate to call depends on the very classes. Make sure to invoke a constructor so that the super class instance will get into a meaningful state.
BIJ001a at 2007-7-13 0:22:16 > top of Java-index,Java Essentials,Java Programming...
# 4

Check the following sample:

public class B{

public B(){

System.out.println("B");

}

}

public class A extends B{

public A(){

System.out.println("A");

}

}

public class Test{

public static void main(String[] args){

A a = new A();

}

}

result:

B

A

surprised?

manuel.leiriaa at 2007-7-13 0:22:16 > top of Java-index,Java Essentials,Java Programming...
# 5

Meanwhile I realized that a constructor in the parent class

and child class is always called, be it implicitly as default

constructor or explicitly as default constructor or other

constructor.

"Meaningful state" - good point. I think my main concerns

go in that direction. On the one hand it's my task to

choose an appropriate super constructor and on the other

hand it's the task of the superclass developer to only offer

constructors which set the class in a meaningful state.

If there are several constructors to choose from, you

can't but know the behaviour of the superclass.

Just_Me_Againa at 2007-7-13 0:22:16 > top of Java-index,Java Essentials,Java Programming...
# 6
To manuel.leiria,Thanks for the example, but I'm actually not surprised since theinstance of the parent class must exist before the child class :-)
Just_Me_Againa at 2007-7-13 0:22:16 > top of Java-index,Java Essentials,Java Programming...
# 7
> To manuel.leiria,> > Thanks for the example, but I'm actually not> surprised since the> instance of the parent class must exist before the> child class :-)Although only one instance actually exists at all - that of the
georgemca at 2007-7-13 0:22:16 > top of Java-index,Java Essentials,Java Programming...
# 8
Yes, although A is instanceof B, there is actually only one instance A :-)
Just_Me_Againa at 2007-7-13 0:22:16 > top of Java-index,Java Essentials,Java Programming...