Java Reference
In-Depth Information
4
5
public
abstract
class
Employee
implements
Payable
{
6
private final
String firstName;
7
private final
String lastName;
8
private final
String socialSecurityNumber;
9
10
// constructor
11
public
Employee(String firstName, String lastName,
12
String socialSecurityNumber)
13
{
14
this.
firstName = firstName;
15
this.
lastName = lastName;
16
this.
socialSecurityNumber = socialSecurityNumber;
17
}
18
19
// return first name
20
public
String getFirstName()
21
{
22
return
firstName;
23
}
24
25
// return last name
26
public
String getLastName()
27
{
28
return
lastName;
29
}
30
31
// return social security number
32
public
String getSocialSecurityNumber()
33
{
34
return
socialSecurityNumber;
35
}
36
37
// return String representation of Employee object
38
@Override
39
public
String toString()
40
{
41
return
String.format(
"%s %s%nsocial security number: %s"
,
42
getFirstName(), getLastName(), getSocialSecurityNumber());
43
}
44
45
// Note: We do not implement Payable method getPaymentAmount here so
// this class must be declared abstract to avoid a compilation error.
46
47
}
// end abstract class Employee
Fig. 10.13
|
Employee
abstract
superclass that implements
Payable
. (Part 2 of 2.)
In Fig. 10.13, we handle this situation differently. Recall that when a class implements
an interface, it makes a
contract
with the compiler stating either that the class will implement
each
method in the interface or the class will be declared
abstract
. Because class
Employee
does not provide a
getPaymentAmount
method, the class must be declared
abstract
. Any
concrete subclass of the
abstract
class
must
implement the interface methods to fulfill the
superclass's contract with the compiler. If the subclass does
not
do so, it too
must
be declared