Method signature and overloading
Method signature
Section titled “Method signature”A method signature consists of:
- Method name
- Parameter types and order (number, types, and sequence)
It does NOT include:
- Return type
- Access modifiers
- Thrown exceptions
public int calculateSum(int a, double b) throws Exception { ... }// Signature: calculateSum(int, double)Method overloading
Section titled “Method overloading”Defining multiple methods with the same name but different parameter lists (different signatures). Return type alone is not sufficient to overload.
Valid overloading:
class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } double add(double a, double b) { return a + b; }}Invalid overloading (only return type differs):
int process(int x) { ... }double process(int x) { ... } // compilation errorOverloading with varargs:
void display(String s) { ... }void display(String... args) { ... } // valid but ambiguous sometimesOverloading and type promotion: Java chooses the most specific method.
void print(int i) { System.out.println("int"); }void print(double d) { System.out.println("double"); }print(5); // calls int versionprint(5.0); // calls double version