ptg7068951
Working with Existing Objects 159
. To refer to a variable of the superclass, as in super.hawaii = 50
. To refer to a method of the superclass, as in super.dragnet()
One way you use the superkeyword is in the constructor method of a sub-
class. Because a subclass inherits the behavior and attributes of its super-
class, you have to associate each constructor method of that subclass with
a constructor method of its superclass. Otherwise, some of the behavior
and attributes might not be set up correctly, and the subclass isn’t able to
function properly.
To associate the constructors, the first statement of a subclass constructor
method must be a call to a constructor method of the superclass. This
requires the superkeyword, as in the following statements:
publicreadFiles(String name, int length) {
super(name, length);
}
This example is the constructor method of a subclass, which is using
super(name, length)to call a comparable constructor in its superclass.
If you don’t use superto call a constructor method in the superclass, Java
automatically calls super()with no arguments when the subclass con-
structor begins. If this superclass constructor doesn’t exist or provides
unexpected behavior, errors result, so it’s better to call a superclass con-
structor yourself.
Working with Existing Objects
OOPencourages reuse.If you develop an object for use with one Java pro-
gramming project, it should be possible to incorporate that object into
another project without modification.
If a Java class is well designed, it’s possible to make that class available for
use in other programs. The more objects available for use in your pro-
grams, the less work you have to do when creating your own software. If
there’s an excellent spell-checking object that suits your needs, you can use
it instead of writing your own. Ideally, you can even give your boss a false
impression about how long it took to add spell-checking functionality to
your project, and use this extra time to make personal long-distance calls
from the office.
NOTE
The author of this book,like
many in his profession,is self-
employed and works out of his
home. Please keep this in mind
when evaluating his advice on
how to conduct yourself in the
workplace.