Method overloading
|
|
No argument int value 33 double value 4.333 Two int values -1 and 7 |
#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);
}
Code in file print.c |
Compile / execute |
---|---|
|
Compiling > cc -o print print.c |
Executing file
> ./print No argument int value 33 |
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.
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)
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:
|
A: |
|