Java Notes

Constructors - super

Every object contains the instance variables of its class. What isn't so obvious is that every object also has all the instance variables of all super classes (parent class, grandparent class, etc). These super class variables must be initialized before the class's instances variables.

Automatic insertion of super class constructor call

When an object is created, it's necessary to call the constructors of all super classes to initialize their fields. Java does this automatically at the beginning if you don't.

For example, the first Point constructor (see Constructors) could be be written

public Point(int xx, int yy) {
    super();  // Automatically inserted
    x = xx;
    y = yy;
}

Explicit call to superclass constructor

Normally, you don't explicitly write the constructor for your parent class, but there are two cases where this is necessary: