Java Reference
In-Depth Information
16 System.out.println(lucy.getVacationForm());
17 lucy.takeDictation("neato");
18 lucy.fileLegalBriefs();
19 }
20 }
The program produces the following output:
Lawyer: 40, $40000.00, 15, pink
I'll see you in court!
Legal Secretary: 40, $45000.00, 10, yellow
Dictating text: neato
I could file all day!
Be careful not to confuse overriding with overloading. Overloading, introduced in
Chapter 3, occurs when one class contains multiple methods that have the same name
but different parameter signatures. Overriding occurs when a subclass substitutes its
own version of an otherwise inherited method that uses exactly the same name and
the same parameters.
9.2 Interacting with the Superclass
The classes in the previous sections demonstrated inheritance in classes containing
only methods. But you'll want to write more meaningful classes that use inheritance
with fields, methods, and constructors. These subclasses require more complex inter-
action with the state and behavior they inherit from their superclass. To show you
how to perform this interaction properly, we'll need to introduce a new keyword
called super .
Calling Overridden Methods
Suppose things are going well at our example legal firm, so the company decides to
give every employee a $10,000 raise. What is the best way to enact this policy change
in the code we've written? We can edit the Employee class and change its
getSalary method to return 50000 instead of 40000. But some of the other types of
employees have salaries higher than the original $40,000 rate (legal secretaries at
$45,000 and marketers at $50,000), and these will need to be raised as well. We'll
end up needing to change several files to enact this single overall raise.
The problem is that our existing code does not represent the relationship between
the various salaries very well. For example, in the LegalSecretary class, instead of
saying that we want to return 45000, we'd like to return the Employee class's salary
plus $5000. You may want to write code like the following, but it does not work,
 
 
Search WWH ::




Custom Search