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
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

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


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