Java Reference
In-Depth Information
objects will inherit copies of the getHours , getSalary , getVacationDays , and
getVacationForm methods, so we won't need to write these methods in the
Secretary class. This will remove the redundancy between the classes.
It's legal and expected for a subclass to add new behavior that wasn't present in
the superclass. We said previously that secretaries add an ability not seen in other
employees: the ability to take dictation. We can add this to our otherwise empty
Secretary class. The following is the complete Secretary class:
1 // A class to represent secretaries.
2 public class Secretary extends Employee {
3 public void takeDictation(String text) {
4 System.out.println("Dictating text: " + text);
5 }
6 }
This concise new version of the Secretary class has the same behavior as the
longer class shown before. Like the two-page specialized manual, this class shows
only the features that are unique to the specific job class. In this case, it is very easy
to see that the unique behavior of secretaries in our system is to take dictation.
The following client code would work with our new Secretary class:
1 public class EmployeeMain {
2 public static void main(String[] args) {
3 System.out.print("Employee: ");
4 Employee edna = new Employee();
5 System.out.print(edna.getHours() + ", ");
6 System.out.printf("$%.2f, ", edna.getSalary());
7 System.out.print(edna.getVacationDays() + ", ");
8 System.out.println(edna.getVacationForm());
9
10 System.out.print("Secretary: ");
11 Secretary stan = new Secretary();
12 System.out.print(stan.getHours() + ", ");
13 System.out.printf("$%.2f, ", stan.getSalary());
14 System.out.print(stan.getVacationDays() + ", ");
15 System.out.println(stan.getVacationForm());
16 stan.takeDictation("hello");
17 }
18 }
The code would produce the following output:
Employee: 40, $40000.00, 10, yellow
Secretary: 40, $40000.00, 10, yellow
Dictating text: hello
Search WWH ::




Custom Search