Pin It

Method Overloading in Java

Introduction

In Java, when we come across a question that what do you mean by method in Java ?. We can simply state its definition saying method is the small block of code that usually performs some specific tasks and help dividing program into specific modules. Generally we need methods because we want to use the concept of code reusability , that means suppose we have written few lines of code which are frequently been used in program. So each time if we want to execute the same code say with different values we wont type again and again. We simply create a method which will have that generic code which can be used by say different arguments passed through it. Thus, it will not impact performance.



What do you mean by Method Overloading in Java ?. 

By Method Overloading, we mean that we can declare methods of same name in a class as long as they have different signatures. By different signatures we mean method can be of same name but with different set of parameters. This difference in parameters can be determined by number , their types and order of their types. Generally, these methods almost perform same actions but they do it with different parameters and their types. Compiler calls an overloaded method based on the number of parameters passed to it, based on different data types used in that and based on order of parameters used.

Example using Method Overloading :- 

Below is the code that demonstrate the concept of method overloading. Here if we look closely to the example we have overloaded method by the name max(). This method is overloaded in the below sample code. If we closely look at the difference of max method declaration. We can see it has been defined by three different ways, lets look into that more closely :-
  • int max(intnum1, intnum2)
  • double max(doublenum1, doublenum2)
  • double max(doublenum1, doublenum2, doublenum3)
In first case, we have max method with two integer parameters. In second case, we have max method with two double parameters. in third case, we have max method with three double parameters. Thus difference in number, data types of parameters and order creates a method which can be overloaded. One note: method overloading has nothing to do with the return types. Generally, return types may or may not be same when we want a method to be overload.

A program to show concept of Method Overloading :


public class MethodOverloading {
  public static void main(String[ ] args) {
    System.out.println("The maximum between 4 and 6 is "
      + max(4, 6));
     
    System.out.println("The maximum between 4.2 and 6.4 is "
      + max(4.2, 6.4));
     
    System.out.println("The maximum between 4.2, 6.4 and 8.6 is "
      + max(4.2, 6.4 , 8.6));
  }
   
  public static int max(int num1, int num2){
    if (num1 > num2)
       return num1;
    else
       return num2;
  }
   
  public static double max(double num1, double num2){
    if (num1 > num2)
       return num1;
    else
       return num2;
  }
   
  public static double max(double num1, double num2, double num3){
    return max( max( num1,num2 ), num3 );
  }

}


Output and explanation of above code:-

Generally, above code is Java code, which performs operation to find the greatest number between the two / three variable. Let us look at the output printed on the console.
The maximum between 4 and 6 is 6
The maximum between 4.2 and 6.4 is 6.4
The maximum between 4.2, 6.4 and 8.6 is 8.6
Generally, in above line of code we have to understand how method max works. In the first case, we have defined two integer argument for max method.It takes two arguments and finds greatest number between the two through if-else construct. In the second case, we have defined two double argument for max method.It takes two arguments and finds greatest number between the two through if-else construct. In the third case, we have defined three double argument for max method.It takes three arguments and finds greatest number between the three through if-else construct and recursion.
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot