Java Reference
In-Depth Information
{
return name + “ “ + address + “ “ + number;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber()
{
return number;
}
}
Listing 8.13
(continued)
Notice that nothing else has changed in this Employee class. The class is
now abstract, but it still has three fields, seven methods, and one constructor.
We cannot instantiate a new Employee, but if we instantiate a new Salary
object, the Salary object will inherit the three fields and seven methods from
Employee. Similarly, an Hourly object will inherit the fields and methods as
well.
The AbstractDemo program in Listing 8.14 contains one compiler error. See
if you can find it, and try to determine the output of the program, assuming
that the statement with the compiler error is omitted.
public class AbstractDemo
{
public static void main(String [] args)
{
Employee s = new Salary(“Thomas Jefferson”, “Monticello, VA”,
3, 2600.00);
Employee h = new Hourly(“John Adams”, “Boston, MA”, 2, 2.50);
Listing 8.14 The AbstractDemo program declares an Employee reference but cannot
instantiate an Employee object.
Search WWH ::




Custom Search