Java Reference
In-Depth Information
Within
main
, the following sequence of events occurs:
1.
The call to
send
on line 21 has a
double
argument, so the next largest compatible data
type of
send
parameters is
Object
on line 6. (Note that as of Java 5.0, primitive types
are autoboxed into their equivalent
Object
type, which for the literal
12.5
is
java.lang.Double
.)
2.
Line 22 invokes the
send
method on line 14 because
123456
is an
int
.
3.
Line 23 invokes the
send
method on line 10 because the argument is a
String
.
4.
Line 24 invokes the
send
method on line 6 because
Date
is a child of
Object
.
Therefore, the output of running
main
in the
Email
class is
Object parameter
int parameter
String parameter
Object parameter
Autoboxing of Primitive Types
Primitive types are automatically boxed into their corresponding wrapper class object
whenever necessary, and they are also unboxed automatically whenever necessary. We
discuss the wrapper classes and autoboxing and unboxing in Chapter 4, “API Contents.”
Method Overriding
The exam objectives state that you should be able to “determine if a method is correctly
overriding another method, and identify legal return values (including covariant returns),
for the method.”
Method overriding
means writing a child class that contains the same
method signature as its parent class. At runtime the child method executes, not the parent
method. The child method takes the place of the parent method, thereby overriding the
behavior of the parent. Method overriding is an important capability of object-oriented
programming, and this section discusses the details of overriding methods in Java.
The rules for overriding an instance method follow:
The method in the child has to have the same signature (name and parameter list) as
the method in the parent.
The access to the child method has to be at least the same or more accessible than the
parent method. For example, if the method is
public
in the parent class, then it has
to be
public
in the child class. A method with default access in the parent could be
public
or
protected
, or have the default access in the child class.
Search WWH ::
Custom Search