Java Reference
In-Depth Information
15
public
String getVacationForm() {
16 return "yellow";
17 }
18 }
Now let's think about implementing the
Secretary
subcategory. As we mentioned
in the previous section, every
Secretary
is also an
Employee
and, consequently,
retains the abilities that
Employee
s have. Secretaries also have one additional ability:
the ability to take dictation. If we wrote
Secretary
as a standalone class, its code
would not reflect this relationship very elegantly. We would be forced to repeat all of
the same methods from
Employee
with identical behavior. Here is the redundant class:
1 // A redundant class to represent secretaries.
2
public class
Secretary {
3
public int
getHours() {
4
return
40;
5 }
6
7
public double
getSalary() {
8
return
40000.0;
9 }
10
11
public int
getVacationDays() {
12
return
10;
13 }
14
15
public
String getVacationForm() {
16
return
"yellow";
17 }
18
19 // this is the only added behavior
20
public void
takeDictation(String text) {
21
System.out.println("Dictating text: " + text);
22 }
23 }
The only code unique to the
Secretary
class is its
takeDictation
method.
What we'd really like to do is to be able to copy the behavior from class
Employee
without rewriting it in the
Secretary
class file.
Search WWH ::
Custom Search