Java Reference
In-Depth Information
The last Employee object in PayDemo is neither a Salary nor Hourly
employee, but simply an Employee object. It can certainly be passed in to
payEmployee():
boss.payEmployee(y);
Notice, though, that the pay is 0.0 for this object, as shown in the output in
Figure 8.3. In a real-world use, we likely would not allow any objects that were
only of type Employee (and not one of the child class types) because there is no
way to determine that employee's pay. I will make this observation again in
the upcoming sections on abstraction.
Because java.lang.Object is a parent of all classes, every object in Java can
take on the form of type Object. There are many instances in the Java API
where Object is used as either a parameter or return value of a method.
For example, the following method signature is found in the
java.util.Vector class:
public Object elementAt(int index)
Because this method declares a return value of type Object, it can return
any type of object it wants; all objects in Java are of type Object. Similarly,
if a method has an Object parameter, any type of object can be passed in
to that parameter. For example, the java.util.Hashtable class contains the
method:
public boolean contains(Object value)
Any reference of any data type can be passed in to this method. In fact,
the only variables that cannot be passed in to the contains() method
are the eight primitive data types. (See the following sidebar titled The
Wrapper Classes .)
Figure 8.3
The output of the PayDemo program.
Search WWH ::




Custom Search