Technical Java. Applications for Science and Engineering
Authors: Palmer G.
Published year: 2003
Pages: 109-110/281
Buy this book on amazon.com >>

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

Method Overriding

Method overriding is when a subclass implements a method with the same name , return type, and parameter list as a method it inherits from a superclass. This differs from method overloading where methods of the same name but different parameter lists are defined. Method overriding is used when a subclass wants to define a method with the same parameters as a superclass method but with a different functionality.

An example of a commonly overridden method is the toString() method defined in the Object class. This method returns a String representation of an object. The default implementation as defined in the Object class simply returns the name of the class and its hash code. The toString() method is overridden by other classes in the Java API to provide a more meaningful description of an instance of the class.

You can call a superclass method that has been overridden using the super keyword.

super.methodName(arguments);

This is useful if you don't want to reinvent a method but instead want to augment its functionality.

Example: Overriding Methods

To demonstrate method overriding we will rewrite the BlackBody3 class from Chapter 8. The BlackBody3 class has access to the default toString() method it inherited from the Object class. The default version from the Object class returns the class name followed by its hash code, which is not very informative. The BlackBody4 class overrides the toString() method to return the values of the temperature and emissivity variables for invoking the BlackBody4 object.

public class BlackBody4
{
  public static final double SIGMA = 5.6697e-12;
  private double temperature, emissivity;

  public BlackBody4(double emiss, double t) {
    emissivity = emiss;
    temperature = t;
  }

  public double getTemperature() {
    return temperature;
  }

  public double getHeating() {
     return SIGMA*emissivity*Math.pow(temperature,4.0);
  }

  public String toString() {
    String str = "Black Body4: temperature=" +
             temperature + " emissivity=" + emissivity;
    return str;
  }
}

Next we will write a driver program that will create a BlackBody4 object and call its toString() method. When an object is concatenated to a String using the + operator, the object's toString() method is automatically called.

public class OverrideDemo
{
  public static void main(String args[]) {

    BlackBody4 body = new BlackBody4(0.85, 2000.0);

    //  Concatenating an object to a String calls the
    //  toString() method associated with the object.

    System.out.println("object is " + body);
  }
}

Output ”

object is BlackBody4: temperature=2000.0 emissivity=0.85

If the toString() method was not overridden, the output would be ”

object is BlackBody4@ea2dfe
Technical Java. Applications for Science and Engineering
Authors: Palmer G.
Published year: 2003
Pages: 109-110/281
Buy this book on amazon.com >>