Method overloading lets you create multiple methods with the same name but different inputs. This helps keep your code neat and easy to understand.
Method overloading in Java
returnType methodName(type1 param1) {
// method body
}
returnType methodName(type1 param1, type2 param2) {
// method body
}
returnType methodName(type1 param1, type2 param2, type3 param3) {
// method body
}All methods share the same name but differ in parameter types or number.
Return type alone cannot distinguish overloaded methods.
add: one adds two numbers, the other adds three.int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; }
print methods: one prints text, the other prints a number.void print(String message) { System.out.println(message); } void print(int number) { System.out.println(number); }
This program shows two greet methods. One says hello with a name. The other adds the person's age.
public class OverloadExample { // Method to greet with a name void greet(String name) { System.out.println("Hello, " + name + "!"); } // Overloaded method to greet with name and age void greet(String name, int age) { System.out.println("Hello, " + name + ". You are " + age + " years old."); } public static void main(String[] args) { OverloadExample example = new OverloadExample(); example.greet("Alice"); example.greet("Bob", 30); } }
Method overloading is decided at compile time based on method signatures.
Changing only the return type does not create an overloaded method.
Use overloading to make your code easier to read and maintain.
Method overloading means having methods with the same name but different parameters.
It helps write cleaner and more flexible code.
Java chooses which method to run based on the inputs you give.
Practice
What is method overloading in Java?
Solution
Step 1: Understand method naming
Method overloading means methods share the same name.Step 2: Check parameter differences
They differ by number or type of parameters, not just return type.Final Answer:
Having multiple methods with the same name but different parameters -> Option DQuick Check:
Same name + different parameters = method overloading [OK]
- Confusing overloading with overriding
- Thinking return type alone overloads method
- Mixing class names with method names
Which of the following is a correct method overloading example in Java?
class Test {
void show(int a) {}
void show(int a, int b) {}
void show(int a) {}
}Solution
Step 1: Check method signatures
Two methods have the same name and same parameter list: show(int a).Step 2: Understand overloading rules
Overloading requires different parameter lists; duplicate signatures cause errors.Final Answer:
Duplicate method show(int a) causes a compile error -> Option BQuick Check:
Same parameters = error, not overloading [OK]
- Assuming return type difference overloads method
- Ignoring duplicate parameter lists
- Thinking method names must differ
What is the output of this Java code?
class Demo {
void print(int a) {
System.out.println("int: " + a);
}
void print(String s) {
System.out.println("String: " + s);
}
}
public class Main {
public static void main(String[] args) {
Demo d = new Demo();
d.print(5);
d.print("hello");
}
}Solution
Step 1: Identify method calls and parameters
First call passes int 5, second call passes String "hello".Step 2: Match calls to overloaded methods
print(int) prints "int: 5", print(String) prints "String: hello".Final Answer:
int: 5 String: hello -> Option AQuick Check:
Correct method chosen by parameter type [OK]
- Mixing output order
- Assuming wrong method called
- Expecting compile error
Find the error in this code snippet:
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
int add(int a, int b) { return a - b; }
}Solution
Step 1: Check method signatures
Two methods have signature add(int a, int b) but different bodies.Step 2: Understand overloading rules
Overloading requires unique parameter lists; duplicate signatures cause errors regardless of body.Final Answer:
Duplicate method add(int a, int b) with different body causes error -> Option AQuick Check:
Same parameters = duplicate method error [OK]
- Thinking different return types fix duplicates
- Ignoring duplicate parameter lists
- Confusing overloading with overriding
Consider this class:
class Converter {
int convert(String s) { return Integer.parseInt(s); }
int convert(int i) { return i * 2; }
int convert(String s, int base) { return Integer.parseInt(s, base); }
}
public class Main {
public static void main(String[] args) {
Converter c = new Converter();
System.out.println(c.convert("10"));
System.out.println(c.convert(10));
System.out.println(c.convert("10", 16));
}
}What is the output when this code runs?
Solution
Step 1: Analyze each method call
convert("10") parses string "10" as decimal 10; convert(10) doubles 10 to 20; convert("10", 16) parses hex "10" as decimal 16.Step 2: Determine output values
Outputs are 10, 20, and 16 respectively.Final Answer:
10 20 16 -> Option CQuick Check:
Overloaded methods handle inputs correctly [OK]
- Confusing hex parsing result
- Expecting compile or runtime errors
- Mixing method outputs
Ready to prove it?
Pass the test and this pattern moves unknown
