Java Reference
In-Depth Information
Answers to Self-Test Exercises
1.
Yes, it will have the instance variables. A derived class has all the instance vari-
ables that the base class has and can add more instance variables besides.
2.
Yes, it will have the methods. A derived class has all the public methods that the base
class has and can also add more methods. If the derived class does not override (re-
define) a method definition, then it performs exactly the same action in the derived
class as it does in the base class. However, the base class can contain an overriding
definition of (a new definition of) a method and the new definition will replace the
old definition (provided it has the same number and types of parameters).
3.
The class DiscountSale will have two methods named getTax and they will have
the following two headings. This is an example of overloading.
public double getTax()
public double getTax( double rate)
4. The method getName is inherited from the class Employee and so needs no defi-
nition. The method getRate is a new method added in the class HourlyEmployee
and so needs to be defined.
5. Yes. You can plug in an object of a derived class for a parameter of the base class
type. An HourlyEmployee is an Employee . A SalariedEmployee is an Employee .
6. public class TitledEmployee extends SalariedEmployee
{
private String title;
public TitledEmployee()
{
super (“no name”, newDate(“January,” 1, 1000), 0 );
title = “No title”;
}
public TitledEmployee(String theName, String theTitle,
Date theDate, double theSalary)
{
super (theName, theDate, theSalary);
title = theTitle;
}
public String getTitle()
{
return title;
}
Search WWH ::




Custom Search