(Method)Overloading

  • Condition

  1. Same method name

  2. Same number and type of parameter

  3. Same return type

long add(int a, long b) {return a+b;}
long add(long a, long b) {return a+b;}
//same type of parameter : duplicated. error
long add(int a, long b) {return a+b;}
long add(long a, int b) {return a+b;}

add(3,3L);
add(3L,3);

//ambiguous error. not clear that call first or second.
add(3,3);
//different return type. error
int add(int a, int b) {return a+b;}
long add(long a, long b) {return a+b;}
  • overloading example : different parameter, same function => method name : verb. ex) add, println, round, random...

: println(int), println(char), println(...)

Last updated