THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

If you create an object of type Y and follow the construction step by step, here are the values of the fields after
each step:


Step What Happens xMask yMask fullMask

0 Fields set to default values 0 0 0

1 Y constructor invoked 0 0 0

2 X constructor invoked (super) 0 0 0

3 Object constructor invoked 0 0 0

4 X field initialization 0x00ff 0 0

5 X constructor executed 0x00ff 0 0x00ff

6 Y field initialization 0x00ff 0xff00 0x00ff

7 Y constructor executed 0x00ff 0xff00 0xffff

Understanding this ordering is important when you invoke methods during construction. When you invoke a
method, you always get the implementation of that method for the actual type of the object. If the method uses
fields of the actual type, they may not have been initialized yet. During step 5, if the constructor X invoked
mask, it would use a fullMask value of 0x00ff, not 0xffff. This is true even though a later invocation
of maskafter the object was completely constructedwould use 0xffff.


Also, imagine that class Y overrides mask with an implementation that explicitly uses the yMask field in its
calculations. If the constructor for X used the mask method, it would actually invoke Y's mask method, and
at that point yMask would be 0 instead of the expected 0xff00.


Methods you invoke during the construction phase of an object must be designed with these factors in mind.
Your constructors should avoid invoking overridable methodsmethods that are not private, static, or
final. If you do invoke such methods, clearly list them in your documentation to alert anyone wanting to
override these methods of their potential unusual use.


Exercise 3.2: Type in the classes X and Y as shown previously, and add print statements to trace the values of
the masks. Add a main method and run it to see the results. (Hint: Use the printf methodshown in Chapter
1with a format specifier of %x to print integers in hexadecimal format.)


Exercise 3.3: If it were critical to set up these masks using the values from the extended class during
construction, how could you work around these problems?


3.3. Inheriting and Redefining Members


When you extend a class you can both add new members to a class and redefine existing members. Exactly
what effect redefining an inherited member has depends on the kind of member. You'll learn about field and
method members here, but we defer discussion of nested members until Chapter 5.

Free download pdf