*Method Overloading
developing the same method with different argument list is called as method overloading. The argument list should differ in either of below 3 ways-
add(int a, int b)
add(double a, int b)
Program ends.
Note-
- Type of arguments should be different.
- Number of argument should be different.
- Position of arg should be different.
class="prettyprint prettyprinted"program startspublic class Demo { public static void main(String[] args) { System.out.println("program starts"); add(5, 10);//this will call add(int a, int b) add(4.6, 10); //this will call add(double a, int b) System.out.println("Program ends."); } public static void add(int a, int b){ System.out.println("add(int a, int b)"); } public static void add(double a, int b){ System.out.println("add(double a, int b)"); } } output ---------
add(int a, int b)
add(double a, int b)
Program ends.
Note-
- Inside a class, both static method as well as non-static method can be overloaded.
- In java, methods can not be overloaded by giving different return type.
- java identify the overloaded method based on the arguments, it does not identify based on the return type nor 'static' keyword.
- There can't be two methods with the same method signature inside a class.