Java Reference
In-Depth Information
9.5 Constructors in Subclasses
As we explained, instantiating a subclass object begins a chain of constructor calls in which
the subclass constructor, before performing its own tasks, explicitly uses super to call one of
the constructors in its direct superclass or implicitly calls the superclass's default or no-argu-
ment constructor. Similarly, if the superclass is derived from another class—true of every
class except Object —the superclass constructor invokes the constructor of the next class up
the hierarchy, and so on. The last constructor called in the chain is always Object 's construc-
tor. The original subclass constructor's body finishes executing last . Each superclass's con-
structor manipulates the superclass instance variables that the subclass object inherits. For
example, consider again the CommissionEmployee - BasePlusCommissionEmployee hierar-
chy from Figs. 9.10-9.11. When an app creates a BasePlusCommissionEmployee object, its
constructor is called. That constructor calls CommissionEmployee 's constructor, which in
turn calls Object 's constructor. Class Object 's constructor has an empty body , so it immedi-
ately returns control to CommissionEmployee 's constructor, which then initializes the
CommissionEmployee instance variables that are part of the BasePlusCommissionEmployee
object. When CommissionEmployee 's constructor completes execution, it returns control to
BasePlusCommissionEmployee 's constructor, which initializes the baseSalary .
Software Engineering Observation 9.9
Java ensures that even if a constructor does not assign a value to an instance variable, the
variable is still initialized to its default value (e.g., 0 for primitive numeric types, false
for boolean s, null for references).
9.6 Class Object
As we discussed earlier in this chapter, all classes in Java inherit directly or indirectly from
class Object (package java.lang ), so its 11 methods (some are overloaded) are inherited
by all other classes. Figure 9.12 summarizes Object 's methods. We discuss several Object
methods throughout this topic (as indicated in Fig. 9.12).
Method
Description
equals
This method compares two objects for equality and returns true if they're equal
and false otherwise. The method takes any Object as an argument. When objects
of a particular class must be compared for equality, the class should override
method equals to compare the contents of the two objects. For the requirements of
implementing this method (which include also overriding method hashCode ), refer
to the method's documentation at docs.oracle.com/javase/7/docs/api/java/
lang/Object.html#equals(java.lang.Object) . The default equals implementa-
tion uses operator == to determine whether two references refer to the same object in
memory. Section 14.3.3 demonstrates class String 's equals method and differenti-
ates between comparing String objects with == and with equals .
hashCode
Hashcodes are int values used for high-speed storage and retrieval of information
stored in a data structure that's known as a hashtable (see Section 16.11). This
method is also called as part of Object 's default toString method implementation.
Fig. 9.12 | Object methods. (Part 1 of 2.)
 
 
 
Search WWH ::




Custom Search