Method OverLoading in Java

The simple definition of Method overloading is to have same method in same class the resides in which has a different signature only. It is always confusing for beginners to understand this concept. Method within the same class that have the same name but different characteristics ( signature) like different number of parameters, which perform related operation are called overloaded methods.

For example :


There is a class with name CompareNumbers :




public class CompareNumbers {

    //main method
    public static void main(String[] args ){
    
    //you can call this method as below
    
    //overloaded method 1 with integer
    int getMax = max(10,20); // method signature with integer parameters.
    
    //overloaded method using double
     
    double getMaxDouble = max(30.0 , 13.00); //same method called, but parameters are different - double
    
    //both method has same name but java would distinguish the method according to their signature and will call appropriate method. That is overloading of method.
    }
    
    //Overloaded method
    public int max(int x, int y ){

     int max;
     if(x > y ) {
      max = x;
      }
     else {
      max= y;
     }
     return max;  //returning max answer to main method
    }
    
    //Now suppose, the data type is different from int ,then you can overload this method like below
    
    //Overloaded method - same name with other method, but parameter is passed as double instead of integer.
    //Java would decide to execute this method depending on signature difference.
    
    public double max(double x, double y ){

     double max;
     if(x > y ) {
      max = x;
      }
     else {
      max= y;
     }
     return max;  //returning max answer to main method
    }

}



When you call  max(3,5) from main method, it will see that both method has same name, but then it will check for signature resemblance,

max(3, 5) = max(integer, integer ) 
here 3 and 5 are both integer so it will call the method with parameter that has integer values.



when this method is called, the 3 will be assigned to x , and 5 will be assigned to y. In the end, after comparison, max value would be returned to main calling method.


public int max(int x, int y ){  // max(3, 5)

int max;
if(x > y ) {
max = x;
}
else {
max= y;
}
return max; //returning max answer to main method
}

also when you call

  double getMaxDouble = max(30.0 , 13.00);  then it will try to find the method with same signature,
 since 30.0 and 13.0 is not an integer , compiler would try to find the method with double parameter.
 so it will call the following method

  double getMaxDouble = max(30.0 , 13.00);   


public double max (double  x = 30.00 , double y =13.00) this method will called and values will be assigned to them as shown in the statement. after it will return the max number to getMaxDouble in the main method.


public double max(double x, double y ){

double max;
if(x > y ) {
max = x;
}
else {
max= y;
}
return max; //returning max answer to main method
}


but after reading this, you probably have question that what if you pass something like this?

max ( 30, 4.0)
so here the rule of conversion type will apply in java.

here are simple rule :
char->int
byte->short->int->long->float->double

any smaller data type, will convert itself into bigger.

max ( 30, 4.0)  = 30 (integer ) and 4.0 (double)   so when you call with this parameter..

as we can see from conversion rule, Integer gets convert into Double value, because Integer is smaller than double.
So here, 30 will be converted to 30.0 and 4.0 and max(double x, double y) method will be called.
The important thing to remember is always smaller gets converted to bigger.
if you convert bigger into smaller, then there is always a chance of losing value or data that's why in compiler it will be converted to smaller to bigger data type.
I hope you guys have understood this.

Ambiguous Overloading

This happens when your overloaded method can be called, but compiler can't decide which method to call.

This image below shows the example of ambiguous overloading.


When you pass max (3,5) both method can be called and conversion rule can be applied,
In this case compiler can't decide which method to choose which would lead to an error by compiler.

To solve above problem, you just need to make another method with both integer parameters.






Theory Of Method Overloading

  • When you give two or more methods the same name within the same class, you are overloading the method name

  • Different method definitions have something different about their parameter lists and java distinguishes the overloaded method according to the number of parameters and types of parameters.

  • When java code is written to call a method, the compiler checks if the first argument has the same type as the first parameter , the second argument has the same type as the second parameter and so on. If no match found, then Java will try to do type conversion. It will convert from smaller data types to bigger data types.

  • Parameter list must be different for overloaded methods.





  • Here i am providing you complete source code to test as you want.


    
    package examples;
    
    public class CompareNumbers {
        
        public static void main(String[] args){
         
         FindMax findMax = new FindMax(); //Create an object of class
         
         double maxofDouble = findMax.max(3.0, 5);  //from cal (object) you can access method of class.
         //type cast conversion rule will be applied on this statement.
         System.out.println("MaxofDouble is " + maxofDouble);
         
         double maxofDouble2 = findMax.max(30, 4.0);  //from cal (object) you can access method of class.
         //type cast conversion rule will be applied on this statement.
         System.out.println("MaxofDouble2 is " + maxofDouble2);
         
         int maxofInt = findMax.max(3, 4);
         System.out.println("MaxofInt is " + maxofInt);
         
        }
        
    }
    
    class FindMax{
        
        public  int max(int x,  int y){
    
         int max;
         if(x > y) {
          max = x;
         }
         else {
          max =y;
         }
         
         return max;
        }
        
        public  double max(double x, double y) {
        
         double max;
         if(x > y) {
          max = x;
         }
         else {
          max =y;
         }
         
         return max;
         
        }
    }
    
    

    you can test by changing the values in the calling method parameter.
    Happy coding!!
    If you guys have any questions, please comment below.

    Thank you.

    Post a Comment

    0 Comments