Saturday, November 22, 2014

Day9 (Java Tutorial)...topis- method overloading.

Please continue from here-
http://selenium-makeiteasy.blogspot.in/2014/11/day8-java-tutorialtopis-this-keyword.html

*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-
  • Type of arguments should be different.
  • Number of argument should be different.
  • Position of arg should be different.
ex-
 class="prettyprint prettyprinted"
public 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
---------
program starts
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.

No comments:

Post a Comment