Method overloading

Figure 249. Method overloading: Same name, different signature Slide presentation Create comment in forum
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 250. Overloading, alternate names Slide presentation Create comment in forum
  • Static polymorphism.

  • Compile time binding.

  • Early binding.


Figure 251. No such concept in C Slide presentation Create comment in forum
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 252. What about return type? Slide presentation Create comment in forum
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 253. Method signatures rationale Slide presentation Create comment in forum

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 254. Method signatures rationale Slide presentation Create comment in forum
Method signatures rationale

exercise No. 86

Will a match be found? Create comment in forum

Q:

We reconsider Figure 249, “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 actually 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 we cannot assign 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() and public void print(int i, int j) expect zero or two arguments respectively 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.

      Thus 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) indeed is being chosen by the overloading mechanism using a «best match» startegie.