Java Reference
In-Depth Information
Here is the output:
Each time getErrorInfo( ) is invoked, a new Err object is created, and a reference to it is
returned to the calling routine. This object is then used within main( ) to display the error
message and severity code.
When an object is returned by a method, it remains in existence until there are no more
references to it. At that point, it is subject to garbage collection. Thus, an object won't be
destroyed just because the method that created it terminates.
Method Overloading
In this section, you will learn about one of Java's most exciting features: method overload-
ing. In Java, two or more methods within the same class can share the same name, as long
as their parameter declarations are different. When this is the case, the methods are said to
be overloaded , and the process is referred to as method overloading . Method overloading
is one of the ways that Java implements polymorphism.
In general, to overload a method, simply declare different versions of it. The compiler
takes care of the rest. You must observe one important restriction: the type and/or number
of the parameters of each overloaded method must differ. It is not sufficient for two meth-
ods to differ only in their return types. (Return types do not provide sufficient information
in all cases for Java to decide which method to use.) Of course, overloaded methods may
differ in their return types, too. When an overloaded method is called, the version of the
method whose parameters match the arguments is executed.
Here is a simple example that illustrates method overloading:
Search WWH ::




Custom Search