Method Overloading in java
Every class has one or more methods which are used to perform different functionalities in the application.
Can we have two methods having the same name in one class?
Yes, we can have two or more methods having the same name in one class, but the condition is their arguments list should be different.
What does it mean by different argument lists?
Argument lists can be different in the following ways.
- No. of parameters passed in the method
- Data type of parameters
- Sequence of data type of parameters
Let’s understand method overloading with examples.
1. Method overloading by changing the no. of arguments:
We have a class companyDetails which is having 2 methods with the same name company, but no. of arguments passed are different.
package com.testingpool; public class CompanyDetails { public static void main(String[] args) { CompanyDetails comp = new CompanyDetails(); comp.company(1234); // calling first method comp.company(1234,"Shekhar"); // calling second method with same name but different arguments } //first method void company(int empid){ System.out.println("Emp id is "+empid); } //second methos with the same name void company(int empid,String empname){ System.out.println("Emp id is "+empid+" and emp name is "+empname); } }
Emp id is 1234 and emp name is Shekhar
2. No. of arguments are same but datatypes of arguments are different:
We have one Addition class which is having 2 methods with the same name getSum. Both the methods have same no. of arguments (i.e. 2) , but their data types of arguments are different.
package com.testingpool; public class Addition { public static void main(String[] args) { Addition sum = new Addition(); sum.getSum(4,5); // calling first method sum.getSum(4.50,4.50); // calling second method with same no. of arguments but different data type } //first method void getSum(int a,int b){ System.out.println(a+b); } //second method with different data type but same no. of arguments void getSum(double a ,double b){ System.out.println(a+b); } }
9.0
3. Sequence of data type of arguments is different:
We have a class StudentDetails which is having same name method called Student. Method Student have the same no. of arguments and dats types, but sequence of data type is different.
package com.testingpool; public class StudentDetails { public static void main(String[] args) { StudentDetails stud = new StudentDetails(); stud.Student(1234,"Madhur"); // calling first method stud.Student("Manan",6789); // calling second method with same no. of arguments but their sequence of datatype is different } //first method void Student(int rollno,String name){ System.out.println("Student's roll no and name :- "+rollno +"--"+name); } //second method with different data type but same no. of arguments void Student(String name ,int rollno){ System.out.println("Student's name and roll no :- "+name +"--"+rollno); } }
Student’s roll no and name :- 1234–Madhur
Student’s name and roll no :- Manan–6789
The reason why Method Overloading is not possible by changing the return type of method?
If there will same methods with changed return data type, then there will be confusion which method to call. There you will get compile time error.
public class Addition { int getsum(int a,int b){ //Compile Time Error System.out.println(a+b); } double getsum(double a,double b){ //Compile Time Error System.out.println(a+b); } public static void main(String args[]){ Addition obj=new Addition(); int result=obj.getsum(20,20); //Compile Time Error } }
Is it possible to overload the main method?
Yes, it is possible to overload the main method. For more detail on main method Click here.
In java method overloading means creating more than one method with same name but with different signature. It is very helpful blog , many thanks.
You’r Welcome!