What is Method Overloading in Java

Last updated on February 27th, 2024

When a class is having multiple methods having the same name but different argument types is called method overloading in Java. The method overloading is also known as Compile-time Polymorphism or Static Polymorphism or Early binding in Java.

Example of Method Overloading

public class Test {
public void methodOne(){
System.out.println("no-arg method");   
}
public void methodOne(int i){
System.out.println("int-arg method");   
}
public void methodOne(double d){
System.out.println("double-arg method");   
}
public static void main(String args[]) {
Test t= new Test();
t.methodOne();
t.methodOne(10);
t.methodOne(10.5);
    }
}

Output

no-arg method
int-arg method
double-arg method

→ In method overloading, the compiler is responsible for performing method resolution based on the reference type. Hence, method overloading is also considered as compile time polymorphism or static polymorphism or early binding.

Automatic promotion in Method Overloading

• In method overloading if the compiler is unable to search the method with an exact match in the class then we will not get any compile time error at that time. 
• Initially, the compiler promotes the argument type to the next level and checks whether the matched method is present in the class or not. If it is available in the class then that method will be considered. If it is not available then the compiler promotes the argument type again to the next level to find the exact match method in the class. The process will be going on until all possible argument promotions still if the matched method is not found in the class then we will get a compile-time error. This process is known as an automatic promotion in method overloading.

automatic_promotion_in_method_overloading

Example of Automatic Promotion in Method Overloading

public class Test {
public void methodOne(int i){
System.out.println("int-arg method");   
}
public void methodOne(float f){
System.out.println("float-arg method");   
}
public static void main(String args[]) {
Test t= new Test();
t.methodOne('a');
t.methodOne(10L);
    }
}

Output

int-arg method
float-arg method

Example of Method Overloading  passing parameter String and Object class

public class Test {
public void methodOne(String s){
System.out.println("String version");   
}
public void methodOne(Object o){
System.out.println("Object version");   
}
public static void main(String args[]) {
Test t= new Test();
t.methodOne("peter");
t.methodOne(new Object());
t.methodOne(null);
   }
}

Output

String version
Object version
String version

→ While resolving method overloading exact match will always get high priority.
→ While resolving the method overloading the child class will get more priority than the parent class.

Example of passing two parameters in Method Overloading

public class Test {
public void methodOne(int i,float f){
System.out.println("int-float method");   
}
public void methodOne(float f,int i){
System.out.println("float-int method");   
}
public static void main(String args[]) {
Test t= new Test();
t.methodOne(10,10.5f);
t.methodOne(10.5f,10);
//t.methodOne(10,10);//error: reference to methodOne is ambiguous,
//both method methodOne(int,float) in Test and method methodOne(float,int) in Test match
//t.methodOne(10.5f,10.5f);// no suitable method found for methodOne(float,float)
//method Test.methodOne(int,float) is not applicable
// (argument mismatch; possible lossy conversion from float to int)
//method Test.methodOne(float,int) is not applicable
//(argument mismatch; possible lossy conversion from float to int)
    }
}

Output

int-float method
float-int method

Example of var-arg parameter in Method Overloading

public class Test {
public void methodOne(int i){
System.out.println("int-arg method");   
}
public void methodOne(int...i){
System.out.println("var-arg method");   
}
public static void main(String args[]) {
Test t= new Test();
t.methodOne();
t.methodOne(10);
t.methodOne(10,20);
   }
}

Output

var-arg method
int-arg method
var-arg method

→ In the method, overloading the var-arg method will get less priority than the exact match. In case no other method is matched then only the var-arg method will get a chance for execution it is almost the same as the default case inside the switch.

Example of Parent and Child classes in Method Overloading

class Animal{
}
class Monkey extends Animal{
}
public class Test {
public void methodOne(Animal a){
System.out.println("Animal version");   
}
public void methodOne(Monkey m){
System.out.println("Monkey version");   
}
public static void main(String args[]) {
Test t= new Test();
Animal a=new Animal();
t.methodOne(a);
Monkey m=new Monkey();
t.methodOne(m);
Animal a1=new Monkey();
t.methodOne(a1);
   }
}

Output

Animal version
Monkey version
Animal version

→ In method overloading resolution is always based on reference type and runtime a object won’t play any role in the method.

Conclusion

In this topic, we learnt about method overloading in Java and its various examples in different cases.

Leave a Comment