spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java / beginning / chap5 / 2 To page 1To page 2To page 3current pageTo page 5To page 6
[previous] [next]

Beginning Java 2 SDK 1.4 Edition

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

Method Overloading

Java allows you to define several methods in a class with the same name, as long as each method has a set of parameters that is unique. This is called method overloading.

The name of a method together with the type and sequence of the parameters form the signature of the method--the signature of each method in a class must be distinct to allow the compiler to determine exactly which method you are calling at any particular point.

Note that the return type has no effect on the signature of a method. You cannot differentiate between two methods just by the return type. This is because the return type is not necessarily apparent when you call a method. For example, suppose you write a statement such as:

Math.round(value);

Although the statement above is pointless since we discard the value that the round() method produces, it does illustrate why the return type cannot be part of the signature for a method. There is no way for the compiler to know from this statement what the return type of the method round() is supposed to be. Thus, if there were several different versions of the method round(), and the return type was the only distinguishing aspect of the method signature, the compiler would be unable to determine which version of round() you wanted to use.

There are many circumstances where it is convenient to use method overloading. You have already seen that the standard class Math contains two versions of the method round(), one that accepts an argument of type float, and the other that accepts an argument of type double. You can see now that method overloading makes this possible. It would be rather tedious to have to use a different name for each version of round() when they both do essentially the same thing. The valueOf() method in the String class is another example. There is a version of this method for each of the basic types. One context in which you will regularly need to use overloading is when you write constructors for your classes, which we'll look at now.

Multiple Constructors

Constructors are methods that can be overloaded, just like any other method in a class. In most situations, you will need to generate objects of a class from different sets of initial defining data. If we just consider our class Sphere, we could conceive of a need to define a Sphere object in a variety of ways. You might well want a constructor that accepted just the (x, y, z) coordinates of a point, and have a Sphere object created with a default radius of 1.0. Another possibility is that you may want to create a default Sphere with a radius of 1.0 positioned at the origin, so no arguments would be specified at all. This requires two constructors in addition to the one we have already written.


home / programming / java / beginning / chap5 / 2 To page 1To page 2To page 3current pageTo page 5To page 6
[previous] [next]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS · Sending an HTML and Plain Text E-newsletter with ASP.NET, Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
ANSI SQL Hierarchical Data Processing Basics · Top 10 Threats to Wireless Security · Nuvio Intros NuvioFlex Virtual PBX

Created: July 1, 2002
Revised: July 1, 2002


URL: http://webreference.com/programming/java/beginning/chap5/2/4.html