Java Reference
In-Depth Information
Self-Test Exercises
14. Redefine the
toString
method of the class
SalariedEmployee
(Display 7.5) so
that it uses
super.toString()
. This new definition of
toString
will be equiva-
lent to the one given in Display 7.5.
15. Redefine the
equals
method for the class
HourlyEmployee
(Display 7.3) using
super.equals
to invoke the
equals
method of the base class
Employee
.
16. Is the following program legal? The relevant classes are defined in Displays 7.2
and 7.3.
public class
EmployeeDemo
{
public static void main( String[] args)
{
HourlyEmployee joe =
new
HourlyEmployee("Joe Young",
new
Date("Feb", 1, 2004), 10.50, 40);
String nameNDate = joe.
super
.toString();
System.out.println(nameNDate);
}
}
17. Suppose you add the following defined constant to the class
Employee
(Display 7.2):
public static final int
STANDARD_HOURS = 160;
//per month
Would it then be legal to add the following method to the class
HourlyEmployee
(Display 7.3)?
public void
setHoursToStandard()
{
hours = STANDARD_HOURS;
}
The Class
Object
Java has a class that is an ancestor of every class. In Java, every class is a derived class of a
derived class of . . . (for some number of iterations of “a derived class of ”) of the class
Object
. So, every object of every class is of type
Object
, as well as being of the type of its
class (and also of the types of all its ancestor classes). Even classes that you define yourself
are descendent classes of the class
Object
. If you do not make your class a derived class of
some class, then Java will automatically make it a derived class of the class
Object
.
The class
Object
allows you to write Java code for methods with a parameter of
type
Object
that can be replaced by an object of any class whatsoever. You will eventu-
ally encounter library methods that accept an argument of type
Object
and hence can
be used with an argument that is an object of absolutely any class.
Object
class