Method overloading in Java programming language
Method overloading in Java programming language
In this tutorial, we discuss Method overloading in Java programming language.
The class has multiple methods with the same name but distinguish parameter in the parameter list. It is called as method overloading in Java.
Method overloading is an Oop concept and is one of the features in Java language. It helps to have many methods with the same name but with some different techniques in the parameter list.
There are three different parameter list
- Number of parameters
- The data type of parameter
- Order of data type in the parameter
When one or more method names are the same, we can use the different number of the parameter with the same data type.
- Different number of parameter
Ex
add(int , int) // method having two parameter as same name add(int , int, int ) // method having three parameter as same name
2. The different data type of parameter
mul(int , int ) mul(int ,float ) //methods having two same number of parameter different data type
3. Different order of data type in the parameter
div(int , float ) div(float, int ) //methods having two same number of parameter different order
Different number of parameter in the argument list
This example shows How method overloading is performed by existing different number of parameter in the argument list
Example
Program 1
class OverloadingEx1{ public static void add(int a, int b,int c){ System.out.println("Total is :"+(a+b+c)); } public static void add(int a, int b){//method 1 System.out.println("Total is :"+(a+b)); } public static void main(String args[]){//method 2 add(45,67);//call method 1 add(45,67,34);//call method 2 }
When the above code is executed, it produces the following results:
Total is : 112 Total is : 146
In the above example, The class have two methods with the name add() but both are having the different number of parameter.
add(int a, int b,int c) add(int a, int b)
So method add() is overloaded based in the different number of parameter
Program 2
class Overloading2{ //class_one public static void add(float a, float b){,//method 1 System.out.println("Total is :"+(a+b)); } public static void add(float a, float b, float c){//method 2 System.out.println("Total is :"+(a+b+c)); } } class OverloadingEx2{//class_two public static void main(String args[]){//main method Overloading2 sum=new Overloading2(); //create object sum.add(34.678f,23.00f); //call method 1 sum.add(26.958f,37.01f,98.011f); //call method 1 } }
When the above code is executed, it produces the following results:
Total is :57.678 Total is :161.979
Difference Data type of parameter in the argument list
This example shows How method overloading is performed by the existing different data type of parameter in the argument list
Example
Program 1
class OverloadingEx3{ public static void add(int a, int b){ System.out.println("Sum of a and b is :"+(a+b)); } public static void add(String fname, String lname){ System.out.println("Full name is :"+fname+lname); } public static void main(String args[]){ add(34,67); add("Robert ","Sam"); } }
When the above code is executed, it produces the following results:
Sum of a and b is : 101 Full name is : Robert Sam
In the above example, The class have two methods with the name add() but both are having the different type of parameter.
add(int a, int b) add(String name, String name)
So method add() is overloaded based in the different type of parameter in the argument list
3. Change Order of data type in the parameter
This example shows How method overloading is performed by existing different order of parameter in the argument list
Program 1
class OverloadingEx4{ public static void method_One(int a, String b){ System.out.println("This is a method one for overloading"); } public static void method_One(String b, int a){ System.out.println("This is a method two for overloading"); } public static void main(String args[]){ method_One(34,"String"); method_One("String",34); } }
When the above code is executed, it produces the following results:
This is a method one for overloading This is a method two for overloading
In the above example, The class have two methods with the name method_One() but both are having a different order of parameter.
method_One(int a, String b) method_One(String b, int a)
So method add() is overloaded based in different order of parameter in the argument list
Program 2
class OverloadingEx4{ public static void add(int a, float b){ System.out.println("Sum of a and b : "+(a+b)); } public static void add(float b, int a){ System.out.println("Sum of a and b : "+(a+b)); } public static void main(String args[]){ add(34,47.76f); add(37.35f,78); } }
When the above code is executed, it produces the following results:
Sum of a and b : 81.759995 Sum of a and b : 115.35
Over view
invalid cases of method overloading
case 1
int add(int a,int b) int add(int c,int d) //same method name and same parameter list
This format is not supported method overloading
Example
//example for return type ,method name and argument list same class MethodOver1{ public int method1(int count1, int count2){ System.out.println(count1+count2); return count1+count2; } public int method1(int var1, int var2){ System.out.println(count1+count2); return count1+count2; } } class disp{ public static void main(String args[]){ MetodOver1 obj=new MetodOver1(); obj.metod1(45,67); obj.metod1(56,66); } }
When the above code is executed, it produces the following results:
compile time error
case 2
int add(int a,int b) float add(int c,int d) //same method name and same parameter list but different return type
This format is not supported method overloading
//example for different return type ,method name and argument list are same class MethodOver2{ public int method1(int count1, int count2){//return type int System.out.println(count1+count2); return count1+count2; } public double method1(int var1, int var2){//return type double System.out.println(count1+count2); return count1+count2; } } class disp{ public static void main(String args[]){ MetodOver1 obj=new MetodOver1(); obj.metod1(45,67); obj.metod1(56,66); } }
When the above code is executed, it produces the following results:
compile time error
Suggested for you
Encapsulation in Java language
Abstract class in Java languag
Method overloading in Java language
Method overriding in Java language
Constructor overloading in Java language
difference to constructor and method in Java
Encapsulation in Java language
How to set a path to Java language