Method Overloading


Method overloading is one of the ways Java implements polymorphism. It allows a number of methods that perform the same general functionality to be given the same name . The C programming language does not support method overloading. That is why the C libraries define the abs() , fabs() , and labs() methods for computing the absolute value of int , double , and long data types. Java defines a single method, Math.abs() , that is overloaded such that it can compute the absolute value of int , long , float , and double data types. Constructors and methods that perform mathematical functions are often overloaded.

Overloading also allows you to extend the capability of inherited methods. A subclass may need additional functionality above what is provided by a method inherited from its superclass. Instead of the subclass defining a new method with a distinct name, the subclass can define an overloaded version of the inherited method.

Overloaded methods share the same name, but must have a different parameter list. The compiler decides which method to call, based on the arguments passed to the method. A method call must be unambiguous. The compiler must be able to decide which overloaded method to call. Return types do not play a role in overloaded method resolution. If an exact match is not found, the compiler will try to call the method that best matches the provided input arguments using whatever type conversions are allowed ( int to float , for instance).

Example: Overloading Methods

The Math2 class implements three overloaded versions of the average() method. The first version returns the average value of two arguments. The second overloaded version returns the average value of three arguments. The third version returns the average value of an array of double values.

 public class Math2 {   public static double average(double a, double b) {     return (a + b)/2.0;   }   public static double average(double a, double b,                                double c) {     return (a + b + c)/3.0;   }   public static double average(double values[]) {     double sum = 0.0;     for(int i=0; i<values.length; ++i) {       sum += values[i];     }     return sum/values.length;   } } 

The main() method of the OverloadDemo class calls each of the three average() methods. The compiler knows which one to call based on the number and type of arguments passed to the method.

 public class OverloadDemo {   public static void main(String args[]) {     double answer;     answer = Math2.average(5.0, 6.8);     System.out.println("average is "+answer);     answer = Math2.average(5.0, 6.8, 9.5);     System.out.println("average is "+answer);     double values[] = {1.2, 2.4, 3.0, 10.2};     answer = Math2.average(values);     System.out.println("average is "+answer);   } } 

Output ”

 average is 5.9 average is 7.1 average is 4.2 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net