Java Reference
In-Depth Information
13 // this is the Lawyer's added behavior
14
public void
sue() {
15 System.out.println("I'll see you in court!");
16 }
17 }
The
LegalSecretary
class could also be written to extend
Employee
, but it has
more in common with
Secretary
. It is legal for a class to extend a class that itself
extends a class, creating a multi-level hierarchy. So we will write the class for
LegalSecretary
as an extension of the class
Secretary
so it inherits the ability to
take dictation:
1 // A class to represent legal secretaries.
2
public class
LegalSecretary
extends
Secretary {
3 // overrides getSalary from Employee class
4
public double
getSalary() {
5
return
45000.0;
6 }
7
8 // new behavior of LegalSecretary objects
9
public void
fileLegalBriefs() {
10 System.out.println("I could file all day!");
11 }
12 }
The following client program uses our
Lawyer
and
LegalSecretary
classes.
Notice that the legal secretary inherits not only the normal employee behavior, but
also the behavior to take dictation from the standard secretary:
1
public class
EmployeeMain2 {
2
public static void
main(String[] args) {
3 System.out.print("Lawyer: ");
4 Lawyer leia =
new
Lawyer();
5 System.out.print(leia.getHours() + ", ");
6 System.out.printf("$%.2f, ", leia.getSalary());
7 System.out.print(leia.getVacationDays() + ", ");
8 System.out.println(leia.getVacationForm());
9 leia.sue();
10
11 System.out.print("Legal Secretary: ");
12 LegalSecretary lucy =
new
LegalSecretary();
13 System.out.print(lucy.getHours() + ", ");
14 System.out.printf("$%.2f, ", lucy.getSalary());
15 System.out.print(lucy.getVacationDays() + ", ");
Search WWH ::
Custom Search