Method Overloading is a feature that allows a single Class to have multiple methods with the exact same name, provided they have different Parameter Lists (also known as Method Signatures).
This is a form of Compile-Time (Static) Polymorphism. It heavily improves code readability. Instead of forcing the developer to remember three different method names like printInt(), printString(), and printDouble(), you just name all of them print(). When you type print(5), the compiler looks at the integer argument and instantly knows which specific method version to link it to.
public class MathUtils {
// Version 1: Adds two integers
public int add(int a, int b) {
return a + b;
}
// Version 2: Overloaded! Adds three integers (Different NUMBER of parameters)
public int add(int a, int b, int c) {
return a + b + c;
}
// Version 3: Overloaded! Adds two doubles (Different TYPE of parameters)
public double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathUtils math = new MathUtils();
// The compiler perfectly resolves which method to trigger
System.out.println(math.add(5, 10)); // Calls Version 1
System.out.println(math.add(5.5, 2.0)); // Calls Version 3
}
}public int compute(int x) and try to overload it by adding public double compute(int x), the compiler will crash! The parameter lists are identical, so the compiler has no idea which one you want when you call compute(5). Return types do NOT play a role in overloading.add(int, double) and add(double, int), and you call add(5, 5), the compiler will throw an error because both arguments are integers, and it cannot safely guess which method you want it to upcast the integers for.Can we overload the main() method in Java?
Yes, you absolutely can! You can write public static void main(int[] args). However, when you execute the program, the JVM strictly searches for the exact signature public static void main(String[] args) to serve as the entry point. The others are just normal static methods.
How does Method Overloading differ from Method Overriding?
Overloading occurs within the SAME class, requires DIFFERENT parameters, and is resolved at COMPILE time. Overriding occurs between Parent/Child classes, requires the EXACT SAME parameters, and is resolved dynamically at RUNTIME.
The native System.out.println() command is the most famous example of Method Overloading in Java. The PrintStream class has over 10 overloaded versions of println to handle ints, doubles, chars, strings, and objects seamlessly.
// All of these use the same method name cleanly
System.out.println(100);
System.out.println("Hello");
System.out.println(true);Test your understanding of Method Overloading with these quick questions.
Question 1
Which of the following is required to successfully overload a method?
Question 2
If you have `void calc(int x)` and you add `int calc(int x)`, what will happen?
Question 3
When does the Java compiler resolve which overloaded method to execute?
Method Overloading is a feature that allows a single Class to have multiple methods with the exact same name, provided they have different Parameter Lists (also known as Method Signatures).
This is a form of Compile-Time (Static) Polymorphism. It heavily improves code readability. Instead of forcing the developer to remember three different method names like printInt(), printString(), and printDouble(), you just name all of them print(). When you type print(5), the compiler looks at the integer argument and instantly knows which specific method version to link it to.
public class MathUtils {
// Version 1: Adds two integers
public int add(int a, int b) {
return a + b;
}
// Version 2: Overloaded! Adds three integers (Different NUMBER of parameters)
public int add(int a, int b, int c) {
return a + b + c;
}
// Version 3: Overloaded! Adds two doubles (Different TYPE of parameters)
public double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathUtils math = new MathUtils();
// The compiler perfectly resolves which method to trigger
System.out.println(math.add(5, 10)); // Calls Version 1
System.out.println(math.add(5.5, 2.0)); // Calls Version 3
}
}public int compute(int x) and try to overload it by adding public double compute(int x), the compiler will crash! The parameter lists are identical, so the compiler has no idea which one you want when you call compute(5). Return types do NOT play a role in overloading.add(int, double) and add(double, int), and you call add(5, 5), the compiler will throw an error because both arguments are integers, and it cannot safely guess which method you want it to upcast the integers for.Can we overload the main() method in Java?
Yes, you absolutely can! You can write public static void main(int[] args). However, when you execute the program, the JVM strictly searches for the exact signature public static void main(String[] args) to serve as the entry point. The others are just normal static methods.
How does Method Overloading differ from Method Overriding?
Overloading occurs within the SAME class, requires DIFFERENT parameters, and is resolved at COMPILE time. Overriding occurs between Parent/Child classes, requires the EXACT SAME parameters, and is resolved dynamically at RUNTIME.
The native System.out.println() command is the most famous example of Method Overloading in Java. The PrintStream class has over 10 overloaded versions of println to handle ints, doubles, chars, strings, and objects seamlessly.
// All of these use the same method name cleanly
System.out.println(100);
System.out.println("Hello");
System.out.println(true);Test your understanding of Method Overloading with these quick questions.
Question 1
Which of the following is required to successfully overload a method?
Question 2
If you have `void calc(int x)` and you add `int calc(int x)`, what will happen?
Question 3
When does the Java compiler resolve which overloaded method to execute?