Java Reference
In-Depth Information
Self-Test Exercises
1. Suppose the class named
DiscountSale
is a derived class of a class called
Sale
.
Suppose the class
Sale
has instance variables named
price
and
numberOfItems
.
Will an object of the class
DiscountSale
also have instance variables named
price
and
numberOfItems
?
2. Suppose the class named
DiscountSale
is a derived class of a class called
Sale
, and
suppose the class
Sale
has public methods named
getTotal
and
getTax
. Will an
object of the class
DiscountSale
have methods named
getTotal
and
getTax
? If so,
do these methods have to perform the exact same actions in the class
DiscountSale
as in the class
Sale
?
3. Suppose the class named
DiscountSale
is a derived class of a class called
Sale
, and
suppose the class
Sale
has a method with the following heading and no other
methods named
getTax
:
public double
getTax()
And suppose the definition of the class
DiscountSale
has a method definition
with the following heading and no other method definitions for methods named
getTax
:
public double
getTax(
double
rate)
How many methods named
getTax
will the class
DiscountSale
have and what are
their headings?
4. The class
HourlyEmployee
(Display 7.3) has methods named
getName
and
getRate
(among others). Why does the definition of the class
HourlyEmployee
contain a def-
inition of the method
getRate
but no definition of the methods
getName
?
There are some restrictions on how you can use the base class constructor call
super
. You cannot use an instance variable as an argument to
super
. Also, the call to
the base class constructor (
super
) must always be the first action taken in a constructor
definition. You cannot use it later in the definition of a constructor.
Notice that you use the keyword
super
to call the constructor of the base class.
You do not use the name of the constructor; you do
not
use
Employee(theName, theDate);
//ILLEGAL
If a constructor definition for a derived class does not include an invocation of a
constructor for the base class, then the no-argument constructor of the base class is
invoked automatically as the first action of the derived class constructor. So, the following