Do not override instance variables . Overriding instance methods is common. Overriding
instance variables —called shadowing rather than overriding— is allowed but
rare. We do not explain the rules for referencing shadowed variables in this text,
and we assume throughout that variables will not be shadowed.
employee described in the folder is an executive, the compensation should be the
salary together with the bonus, not just the salary alone.
We capture this important point about object-oriented programming in the
following rule. The rule is written in such a way that it will work in later cases,
when we discuss subclasses of subclasses.
Public method overriding rule . When determining the instance
method to call (within a folder) for a call method-name ( argu-
ments ) , start at the bottom of the folder and search upward until
the appropriate method is found.
Searching from the bottom upward ensures that the overriding method is called.
Notice that all the instance variables are private; they cannot be seen outside
the class in which they are defined. In fact, variables name , start , and salary
cannot be referenced in the methods of subclass Executive ! However, their val-
ues can be retrieved using public, inherited, getter methods.
Using super to get at an overridden method
Look at function getCompensation in Fig. 4.1. It is supposed to yield the
sum of the salary and the bonus . How is the salary retrieved? The function
body cannot reference the field because it is private. Moreover, a call
getCompensation() within the function would call itself, since it is the over-
Employee(String, int )
changeSalary( double )
setStart( int )
Executive(String, int , double )
An instance of subclass Executive