Java syntax: call inherited function/procedure
super .m( arguments )
Example: super .toString()
Purpose : To call method m of the superclass. Use
only when an overriding method was defined and
the superclass method is wanted.
Java syntax: call superclass constructor
super ( arguments );
Example : super ("Gries", 1966, 2500);
Purpose : Call a constructor of the super-
class. Can be used only as the first state-
ment of a constructor body.
This comment is a reminder to whomever is writing or maintaining the class (or
simply reading it) to keep this relation true. The relation is called an invariant
because it is unchanging; it is “invariantly true” in each instance. Field salary
always contains the product of the hourly pay and the hours worked.
The constructor has a call on class changeSalary of the superclass, which
truthifies the class invariant initially. And, since none of the other methods of the
class change the hours worked, pay per hour, or salary, it remains true.
Of course, there should be some way to change the hours worked or pay per
hour. Self-help exercises ask you to write setter methods for these fields. In writ-
ing them, the class invariant reminds you to change field salary whenever you
change the hours or the pay per hour. Without the class invariant, you would not
know that this had to be done, and this would lead to a logical error in the class.
In any class that has several fields, there may be some relationship among
their values that has to be maintained. It is important to express this relationship
as a comment near the declarations of the fields and to make the comment thor-
ough, precise, and clear. You, the programmer, will rely on it when implement-
ing the methods of the class, and others will have an easier time reading and
understanding the class.
Constructors in a subclass
The purpose of a constructor in any class is to initialize some (or all) of the fields
of an instance when the instance is created. In class Executive , four fields have
to be initialized: three inherited fields and field bonus .
A general rule is to initialize inherited fields using a constructor of the super-
class. In class Executive , we have to do this because the inherited fields are pri-
vate. But even if they were public, it would be better to use a superclass con-
structor to initialize them. Since they are declared in the superclass, let the super-
class take care of them. This policy helps isolate parts of the program and makes
the program easier to maintain.
Within a constructor, a call to another constructor must be the first statement
of the body. That makes sense; initialize the inherited fields before the newly
In the constructor of class Executive , the first thought on calling the super-
class constructor would be to write: