• Struts
  • Java IO
  • Core Java
  • SQL Tutorial
  • Servlet
  • Spring
  • RubyonRails
  • jQuery
  • PHP

  • <

    Java Method Overloading Tutorial

    Whenever same method name is exiting multiple times in the same class with different number of parameter or different order of parameters or different types of parameters is known as method overloading.

    Method overloading is used to easily figure out the program.


    Java Method Overloading Example



    
    public class mainclass 
    {
    public static void main(String[] args) 
    { 
    System.out.println("Execution starts from Main()"); 
    } 
    void main(int args) 
    { 
    System.out.println("Override main()"); 
    } 
    double main(int i, double d) 
    {
    System.out.println("Override main()"); 
    return d; 
    } 
    }
    

    Output:
    Execution starts from Main()
    

    Java Method Overloading Example 2



    class Addition 
    { 
    void sum(int a, int b) 
    { 
    System.out.println(a+b); 
    } 
    void sum(float a, float b) 
    { 
    System.out.println(a+b);  
    } 
    public static void main(String args[]) 
    { 
    Addition obj=new Addition(); 
    obj.sum(10, 20); 
    obj.sum(10.05, 15.20);  
    }
    }
    

    Output:
    30
    25.
    25
    

    Java Method Overloading Example 3



    class Addition 
    { 
    void sum(int a, int b) 
    { 
    System.out.println(a+b); 
    } 
    void sum(int a, int b, int c) 
    { 
    System.out.println(a+b+c); 
    } 
    public static void main(String args[]) 
    { 
    Addition obj=new Addition(); 
    obj.sum(10, 20); 
    obj.sum(10, 20, 30); 
    } 
    }
    

    Output:
    30
    60
    














    © copyright 2017-2021 Completedone pvt ltd.