Monday, 24 November 2014

Method overloading in java

If two or more methods have same name , but different argument then it is called method overloading.

Why you would do that (same name but different argument)?

Lets take an example. You want to print salary of employee and sometimes company gives bonus to their employee and sometimes it don’t.So If company don’t give bonus then we can use printSalary(int salary) method and if it provides bonus then we can use printSalary(int salary,int bonus) so both methods are doing same work but their inputs are different so it will increase readability of programs.Otherwise if you give different methods name,it will become hard to understand.
view plainprint?
  1. package org.arpit.java2blog;
  2. public class Employee{
  3.     public void printSalary(int salary)
  4.     {
  5.         System.out.println(“Salary without bonus : “+salary);
  6.     }
  7.     public void printSalary(int salary,double bonus)
  8.     {
  9.         System.out.println(“Salary with bonus : “+(salary+bonus));
  10.     }
  11.     public static void main(String args[])
  12.     {
  13.         Employee e1=new Employee();
  14.         // if no bonus provided, we can use this method
  15.         e1.printSalary(20000);
  16.         System.out.println(“**********************”);
  17.         // If bonus provided we can pass to overloaded method and add to salary
  18.         e1.printSalary(2000010000);
  19.     }
  20. }
When you run above program, you will get following output:
view plainprint?
  1. Salary without bonus : 20000
  2. **********************
  3. Salary with bonus : 30000

Rules of Method overloading :

Number of Arguments Overloaded method can have different number of arguments
Date type Overload method can have different data type for argument
Return type Return type can be changed but either number of argument or data type of argument should also be changed..
Order of arguments If you change sequence of arguments then it is also valid method overloading provided you have different data types arguments.
Constructor Can be overloaded
So you can overload method using three ways:
  1. By changing number of arguments
  2. By changing data type of arguments
  3. By changing sequence of arguments if they are of different types

By changing number of arguments:

Above example which we have taken is of this type.We are overloading printSalary() method with different number of argument.

By changing data type of arguments:

In above example, we will create another method, which will take double data type as input.
view plainprint?
  1. package org.arpit.java2blog;
  2. public class Employee{
  3.     public void printSalary(int salary)
  4.     {
  5.         System.out.println(“Salary without bonus : “+salary);
  6.     }
  7.     public void printSalary(int salary,double bonus)
  8.     {
  9.         System.out.println(“Salary with bonus : “+(salary+bonus));
  10.     }
  11.   public void printSalary(double salary)    {
  12.         System.out.println(“Salary without bonus : “+salary);
  13.     }
  14.     public static void main(String args[])
  15.     {
  16.          Employee e1=new Employee();
  17.         // if no bonus provided, we can use this method
  18.         //will call printSalary(int)
  19.         e1.printSalary(20000);
  20.         Employee e2=new Employee();
  21.         // will call printSalary(double)
  22.         e2.printSalary(30000.5);
  23.         System.out.println(“**********************”);
  24.         // If bonus provided we can pass to overloaded method and add to salary
  25.         e1.printSalary(2000010000);
  26.     }
  27. }

 so here we have introduced a new method which takes double datatype as input.This is also valid method overloading.

By changing sequence of argument if they are of different data types:

We can introduce a new method printSalary(double bonus,int salary). so by changing order of argument we can overload method.
view plainprint?
  1. package org.arpit.java2blog;
  2. public class Employee{
  3.     public void printSalary(int salary)
  4.     {
  5.         System.out.println(“Salary without bonus : “+salary);
  6.     }
  7.     public void printSalary(int salary,double bonus)
  8.     {
  9.         System.out.println(“Salary with bonus : “+(salary+bonus));
  10.     }
  11.  public void printSalary(double bonus,int salary)
  12.     {
  13.         System.out.println(“Salary with bonus : “+(salary+bonus));
  14.     }
  15.     public static void main(String args[])
  16.     {
  17.         Employee e1=new Employee();
  18.         // if no bonus provided, we can use this method
  19.         e1.printSalary(20000);
  20.         System.out.println(“**********************”);
  21.         // If bonus provided we can pass to overloaded method and add to salary
  22.         e1.printSalary(2000010000);<
  23.       // Changing sequence 
  24.         e1.printSalary(2000.520000);
  25.     }
  26. }

Why we can’t change only return type?

 If we change only return type, it will become ambiguous for compiler to figure out which method to call.That is why you can not change only return type.

What is static binding?

When you compile Java program. During compilation process, compiler bind method call to actual method. This is called static binding and method overloading binding happens at compile time.

For more Visit: http://www.quontrasolutions.com/blog/category/java

No comments:

Post a Comment