In other words, we can say that Method overloading is a concept of Java in which we can create multiple methods of the same name in the same class, and all methods work in different ways. When more than one method of the same name is created in a Class, this type of method is called the Overloaded Method.
Case 1-: Automatic promotion in overloading while resolving overloaded methods if exact match method not available then we won't get any compile time error immediately.
- It will promote argument to the next level and check whether matched method are available are not.
- In this case, if the matched method is available then it will be considered and if the matched method is not available then the compiler promotes the argument once again to the next level. If a matched method is not available, then we will get a compile time error.
- The following are all possible promotions in overloading.
Example-:
class Test{
public static void m(int i){
System.out.print("int-agrs");
}
public static void m(float i){
System.out.print("float-agrs");
}
public static void main(String... args){
Test t=new Test();
t.m(10);//int-args
t.m(10.5f);// float-args
t.m('a'); // int-args
t.m(10L);//float-args
t.m(10.5);// compile time error cannot find symbol method m(double) Test
}
}
Case 2-:while resolving overloaded method, compiler always keep precedence for child type argument when compared with parent type argument.
class Test{
public static void m(String str){
System.out.print(“String version”);
}
public static void m(Object o){
System.out.print(“Object-version”);
}
public static void main(String... args){
Test t=new Test();
t.m(new Object);//Object Version
t.m("Harsh");// String-version
t.m(null);//String-version
}
}
Case 3-:
class Test{
public static void m(String str){
System.out.print("String-version");
}
public static void m(StringBuffer sb){
System.out.print("StringBuffer");
}
public static void main(String... args){
Test t=new Test();
t.m("Harsh"); //String-version
t.m(new StringBufffer("Harsh")); //StringBuffer version
t.m(null); //compile time error reference to m() is ambiguous
}
}
Case 4-:
class Test{
public static void m(int i,float f){
System.out.print("int-float");
}
public static void m(float y,int x){
System.out.print("float-int");
}
public static void main(String... args){
Test t=new Test();
t.m(10,10.4f);//int-float
t.m(10.5f,10);// float-int
t.m(10,10); //compile time error reference to m() is ambiguous
t.m(10.5f,10.5f); //compile time error reference to m() is ambiguous
}
}
Case 5-: In general var-agr method will get the least priority that is if no other method matched then only var-agr method will get the chance it is exactly same as default case inside switch.
class Test{
public static void m(int i){
System.out.print("Genral method");
}
public static void m(int ... i){
System.out.print("var-args method");
}
public static void main(String... args){
Test t=new Test();
t.m(10);//Genral method
t.m();// var-args method
t.m(10,20); // var-args method
}
}
Case 6-:In overloading method, resolving always takes care by compiler based on reference type. In overloading, run type object won't play any role.
class Animal{
}
class monkey extends Animal{
}
class Test{
public void m(Animal a){
System.out.println("animal-version");
}
public void m(monkey m){
System.out.println("monkey-version");
}
public static void main(String... args){
Test t=new Test();
Animal a=new Animal();
t.m(a); // animal-version
monkey m=new monkey();
t.m(m); // monkey-version
Animal a1=new monkey();
a1.m(a1); //Animal-version
}
}
Case 7-:In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.
class Test{
public static int m(int x,int y){
return x+y;
}
public static double m(int x,int y ,int z){
return x+y+z;
}
public static void main(String... args){
System.out.print(m(10,10));
}
}
Can we overload Java main() method?
Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only. Let's see the simple example:
class Test{
public static void main(String[] args){
System.out.println("main with String[]");
}
public static void main(String args){
System.out.println("main with String");
}
public static void main(){
System.out.println("main without args");
}
}
Difference Between overloading and overriding -: