Programming and Problem Solving with Java

(やまだぃちぅ) #1
7.6 Implementing a Derived Class | 347

Constructors in Derived Classes


What would happen if we forgot to include a constructor in the declaration of a derived
class? Java automatically provides a default constructor that calls the superclass construc-
tor with an empty parameter list. We mentioned this issue briefly in Chapter 5.
In fact, Java requires every constructor in a derived class to call a constructor in its su-
perclass. That call must be the first statement appearing in the constructor even before any
declarations. If it isn’t the first statement, Java automatically inserts the same default call.
Java requires us to call a superclass constructor in this way because every derived class
is a specialized form of its superclass. An object of the subclass can be assigned to a variable
of both its own class and its superclass (and by extension, any of the classes above it in the
hierarchy). The superclass may perform initialization operations to create a valid object.
Rather than requiring every derived class to duplicate those operations, Java simply enforces
the rule that one of the superclass constructors must be called, either explicitly in our con-
structor or implicitly by default.
Of course, the superclass constructor must call a constructor for its superclass, and so on
until the constructor forObjectis called.Thus the process of instantiating an object calls a chain
of constructors that provides essential initialization all the way up toObject. Next, we examine
how Java identifies methods and the Java syntax for calling a constructor in the superclass.


Overloading and Method Signatures


If you were to examine the interface of the class JTextField, you would discover that it has
not one, but five constructors:


publicJTextField()
publicJTextField(String text)
publicJTextField(int columns)
publicJTextField(String text, int columns)
publicJTextField(Document doc, String text, int columns)


We’ve always been careful to avoid declaring duplicate identifiers in our pro-
grams, yetJTextFieldhas five constructors, all with the same name. How is this pos-
sible? In the case of methods, Java uses more than just the name to identify them; it
also uses the parameter list. A method’s name, the number and types of parameters
that are passed to it, and the arrangement of the different parameter types within
the list combine into what Java calls thesignatureof the method.
Java allows us to use the name of a method as many times as we wish, as long
as each one has a different signature. When we use a method name more than
once, we are overloadingits identifier. The Java compiler needs to be able to look
at a method call and determine which version of the method to invoke. The five
constructors in the class JTextField, for instance, all have different signatures: the
first constructor takes no arguments, the second takes a String, the third takes an int, the
fourth takes both a Stringand an int, and the fifth takes an object of class Document,a String,


Signature The distinguishing
features of a method heading;
the combination of the method
name with the number and
type(s) of its parameters in their
given order
Overloading The repeated
use of a method name with a
different signature
Free download pdf