Method overloading

Figure 223. Method overloading: Same name, different signature Slide presentation
public class Print {
  public void print() {             // (void)
    System.out.println("No argument");
  }
  public void print(int i) {        // (int)
    System.out.println("int value " + i);
  }
  public void print(double d) {     // (double)
    System.out.println("double value " + d);
  }
  public void print(int i, int j) { // (int, int)
      System.out.println("Two int values "+
         i + " and " + j);
  }
}
Print p = new Print();
p.print();
p.print(33);
p.print(4.333);
p.print(-1, 7);
No argument
int value 33
double value 4.333
Two int values -1 and 7

Figure 224. Overloading, alternate names Slide presentation
  • Static polymorphism.

  • Compile time binding.

  • Early binding.


Figure 225. No overloading in »C« Slide presentation
#include <stdio.h>

void print() {
  printf("No argument\n");
}

void print(int i) {              /* Error: redefinition of ‘print’ */
  printf("int value %d\n", i);
}

void main(void) {
  print();
  print(33);
}

Figure 226. »C« requires unique function names Slide presentation
Code in file print.c Compile / execute
#include <stdio.h>

void print() {
  printf("No argument\n");
}

/* Different function name */
void printIntValue(int i) {   
  printf("int value %d\n", i);
}

void main(void) {
  print();
  printIntValue(33);
}

Compiling print.c to executable file print

> cc -o print print.c

Executing file print

> ./print
No argument
int value 33

Figure 227. No distinction on return type Slide presentation
public class Person {
  String getDetails() { return "dummy";}
  int getDetails() { return 1;} // Error: 'getDetails()' is already
}                                // defined in 'Person'
Return type Method signature
Method name Argument type list
String getDetails (void)
int getDetails (void)

Only method signature support in Java ignoring return type.


Figure 228. Method signatures rationale Slide presentation

In Java method signatures allow for uniquely addressing a method within a given class e.g.:

The method named print having an int argument followed by a double:

print(int, double)

Figure 229. Method signatures rationale Slide presentation
Method signatures rationale

exercise No. 89

Will a match be found?

Q:

We reconsider Figure 223, “Method overloading: Same name, different signature ” adding the following statements:

Print p = new Print();
final long value = 44L;
p.print(value);

Answer the following questions:

  1. Does the Print class provide a method matching exactly?

  2. Read Identify Potentially Applicable Methods and determine the set of potentially matching methods with respect to p.print(44L).

  3. Test your result from the previous step by executing the given sample code.

A:

  1. The Print class does not provide an exactly matching method. The method signature print(long) is absent:

    // Not being defined in class Print
    
    public void print(long l) {   
      System.out.println("long value " + l);
    }
  2. We create the set of potentially matching methods by exclusion:

    1. public void print() does not accept any argument at all and thus does not match.

    2. public void print(int i) accepts a single argument of type int. But even using final does not allow for assigning an eight byte long to a four byte int:

      final long value = 44L;
      
      int a = value; //Incompatible types. Required: int Found: long

      Therefore public void print(int i) does not match either.

    3. public void print(int i, int j) expects two arguments rather then one being provided by p.print(44L) and thus do not match.

    4. We may assign a long to a double by virtue of widening:

      final long value = 44L;
      
      final double d = value; // widening, o.K.

      So public void print(double d) potentially matches.

    Thus public void print(double d) is the only matching candidate.

  3. Execution yields:

    double value 44.0

    Thus public void print(double d) is being chosen by the compiler overloading mechanism's «best match» strategy.