Java Reference
In-Depth Information
Employee(n, d);
but that is not the syntax Java uses to call the superclass constructor. Instead, use:
super (n, d);
Thus, to call a constructor of the superclass, write a conventional call to the
superclass constructor but replace the name of the class name with super . Note
that there is no period following keyword super , as there is in the other use of
super (see Sec. 4.1.1).
If you do not call a superclass constructor
The first statement of a subclass constructor must be a call on a superclass
constructor. If you do not put one in, Java inserts this one:
super ();
In this case, the superclass must have a constructor with no parameters —if
not, the program is illegal and a syntactic-error message will appear when you
try to compile the program. The error message may be so confusing that you have
trouble determining what the mistake is —unless you remember the rules that
Java inserts a superconstructor call if you do not. This error will happen more
often than you expect, unless you get in the habit of explicitly writing a call to
the superclass constructor as the first statement in the subclass constructor.
4.2
Casting about, or about casting
4.2.1
Apparent and real classes
An instance of class Executive of Fig. 4.1 can be created and stored using an
initializing assignment like
Lesson
page 4-3
Executive c= new Executive("Gries", 1966, 25000);
An executive is an employee; in the same way, we say that an instance of class
Executive , like c , is also an Employee . And, we can assign it to an Employee
variable:
a1
a1
Employee
Employee
c a1
d a1
toString() ...
toString() ...
apparent
type:
Executive
apparent
type:
Employee
Executive
Executive
toString() getBonus() ...
toString() getBonus() etc.
Figure 4.5:
Two different views of the same object
Search WWH ::




Custom Search