If two or more methods have same name , but different argument then it is called
When you run above program, you will get following output:
method overloading
.Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog; public class Employee{       public void printSalary(int salary)    {       System.out.println("Salary without bonus : "+salary);          }       public void printSalary(int salary,double bonus)    {       System.out.println("Salary with bonus : "+(salary+bonus));          }    public static void main(String args[])    {             Employee e1=new Employee();       // if no bonus provided, we can use this method       e1.printSalary(20000);       System.out.println("**********************");       // If bonus provided we can pass to overloaded method and add to salary       e1.printSalary(20000, 10000);    } } |
Salary without bonus : 20000
**********************
Salary with bonus : 30000
**********************
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:
- By changing number of arguments
- By changing data type of arguments
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package org.arpit.java2blog; public class Employee{ Â Â Â Â Â Â public void printSalary(int salary) Â Â Â { Â Â Â Â Â Â System.out.println("Salary without bonus : "+salary); Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â public void printSalary(int salary,double bonus) Â Â Â { Â Â Â Â Â Â System.out.println("Salary with bonus : "+(salary+bonus)); Â Â Â Â Â Â Â Â Â } Â public void printSalary(double salary)Â Â Â { Â Â Â Â Â Â System.out.println("Salary without bonus : "+salary); Â Â Â Â Â Â Â Â Â } Â Â Â public static void main(String args[]) Â Â Â { Â Â Â Â Â Â Â Â Â Â Â Â Employee e1=new Employee(); // if no bonus provided, we can use this method //will call printSalary(int) e1.printSalary(20000); Employee e2=new Employee(); // will call printSalary(double) e2.printSalary(30000.5); System.out.println("**********************"); // If bonus provided we can pass to overloaded method and add to salary e1.printSalary(20000, 10000); Â Â Â } } |
 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package org.arpit.java2blog; public class Employee{       public void printSalary(int salary)    {       System.out.println("Salary without bonus : "+salary);          }       public void printSalary(int salary,double bonus)    {       System.out.println("Salary with bonus : "+(salary+bonus));          }  public void printSalary(double bonus,int salary)    {       System.out.println("Salary with bonus : "+(salary+bonus));          }    public static void main(String args[])    {             Employee e1=new Employee();       // if no bonus provided, we can use this method       e1.printSalary(20000);       System.out.println("**********************");       // If bonus provided we can pass to overloaded method and add to salary       e1.printSalary(20000, 10000);<      // Changing sequence        e1.printSalary(2000.5, 20000);    } } |
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.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.