Method Overloading

    Method Overloading

    Method overloading is an object-oriented concept in which a class has many methods with the same name but having different parameters. This will increase the readability of the program. You can overload the methods by either changing the number of arguments used or by changing the data type of the arguments.

    Method overloading by changing the number of arguments-

    In the below example we are using two add() methods but with a different number of parameters. These methods are static so you do not have to create an instance for these methods.

    Example-

    class Addition{  
       static int add(int a,int b){return a+b;}  
       static int add(int a,int b,int c){return a+b+c;}  
    }  
    
    class Demo{  
       public static void main(String[] args){  
         System.out.println(Addition.add(1,2));  
         System.out.println(Addition.add(1,2,3));  
       }
    }  

    Output-

    3
    6

    Method overloading using different data types of the arguments-

    In the below example we are using two add90 methods but with different data type arguments.

    Example-

    class Addition{  
        static int add(int a, int b){return a+b;}  
        static double add(double a, double b){return a+b;}  
    }  
    
    class Demo{  
        public static void main(String[] args){  
          System.out.println(Addition.add(1,2));  
          System.out.println(Addition.add(12.3,12.6));  
        }
    }  

    Output-

    3
    24.9

    Note :

    We cannot achieve method overloading if we keep the definition of the method the same including a number of parameters and the data type of the parameters but changing the return type of the methods. This will create ambiguity for the compiler and you will get a compile-time error.

    Example-

    class Addition{  
        static int add(int a,int b){return a+b;}  
        static double add(int a,int b){return a+b;}  
    }  
    
    class Demo{  
        public static void main(String[] args){  
           System.out.println(Addition.add(1,2));
        }
    } 

    Output-

    Compile Time Error: method add(int,int) is already defined in class Addition

    Overloading main() method

    Yes, it is possible for overloading the main() method. But the main() method with the staring array as arguments will be called by the JVM.

    Example-

    class Demo{  
        public static void main(String[] args){System.out.println("main with String[]");}  
       
        public static void main(String args){System.out.println("main with String");}  
        
        public static void main(){System.out.println("main without args");}  
    }  

    Output-

    main with String[]

    Implementing type promotion with Method overloading

    If no match for the data type is found then one data type will get promoted to another data type implicitly by JVM. for example- byte can be promoted to short, long, int, double or float. Short data type to int, long, float or double. Char data type to int, long, float or double.

    Example-

    class Demo{  
    
      void sum(int a,long 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[]){  
    
      Demo obj=new Demo();  
    
      obj.sum(20,20);// second int literal will be promoted to long implicitly
    
      obj.sum(2,2,2);  
      }  
    }  

    Output-

    40
    6

    Implementing type promotion with Method Overloading (case: method match found)

    If the data type of the arguments matches with the parameter data type, then type promotion will not be performed.

    Example-

    class Demo{  
      void sum(int a,int b){System.out.println("matched method invoked");}  
    
      void sum(long a,long b){System.out.println("long method invoked");}  
    
      public static void main(String args[]){  
    
      Demo obj=new Demo();  
    
      obj.sum(20,20);// int sum() method gets invoked  
    
      }  
    
    }  

    Output-

    Matched method invoked

    Implementing type promotion with Method Overloading (case: method match not found)

    If there is no match found then the implicit type promotion will not be performed.

    Example-

    class Demo{  
    
      void sum(int a,long b){System.out.println("first method invoked");}  
    
      void sum(long a,int b){System.out.println("second method invoked");}  
    
      
    
      public static void main(String args[]){  
    
      Demo obj=new Demo();  
    
      obj.sum(20,20);// ambiguity  
    
      }  
    
    }  

    Output-

    Compile-time error