Today I practiced some tricky scenarios of Java Method Overloading.
Method overloading is a very common concept in Java, but the way Java decides which method to call can sometimes be confusing.
So I explored 15 scenarios to understand how Java selects the correct method during compile time.
πΉ What is Method Overloading?
Method Overloading means:
β Same method name
β Different parameters (type / number / order)
Example:
class Test1 {
void show(int a){
System.out.println("int method called");
}
void show(double a){
System.out.println("double method called");
}
public static void main(String[] args) {
Test1 t = new Test1();
t.show(10);
}
}
Explanation
10 is an int literal, so Java finds the exact match.
Output
int method called
π§ Java Method Selection Priority
When multiple overloaded methods exist, Java follows this priority order:
1οΈβ£ Exact Match
2οΈβ£ Widening
3οΈβ£ Autoboxing
4οΈβ£ Varargs
π§ Java Method Overloading Priority Table
| Priority | Rule | Description | Example |
| --------------- | --------------- | ------------------------------------------------------------------------------------- | ------------------------------ |
| 1οΈβ£ Highest | Exact Match | Java first looks for a method whose parameter type exactly matches the argument type. | `show(int a)` called with `10` |
| 2οΈβ£ | Widening | If exact match is not found, Java converts the argument to a larger compatible type. | `int β long`, `int β double` |
| 3οΈβ£ | Autoboxing | Java converts primitive types into wrapper classes automatically. | `int β Integer` |
| 4οΈβ£ Lowest | Varargs | If none of the above matches, Java chooses the variable-length argument method. | `show(int... a)` |
π Quick Example of the Priority
| Method Available | Call | Java Chooses |
| ------------------------------- | ---------- | -------------------------------------- |
| `show(int)` and `show(long)` | `show(10)` | `show(int)` (Exact Match) |
| `show(long)` and `show(double)` | `show(10)` | `show(long)` (Widening) |
| `show(Integer)` and `show(int)` | `show(10)` | `show(int)` (Exact Match > Autoboxing) |
| `show(int)` and `show(int...)` | `show(10)` | `show(int)` (Exact Match > Varargs) |
β οΈ Special Case: Ambiguous Method
| Scenario | Result |
| ------------------------------------------------------------- | ---------------------- |
| Two methods match equally | **Compile Time Error** |
| Example: `show(String)` and `show(Integer)` with `show(null)` | Java cannot decide |
β
One-line Memory Trick
`Exact Match β Widening β Autoboxing β Varargs`
π Scenario 1
class Test1 {
void show(int a){
System.out.println("int method called: " + a);
}
void show(long a){
System.out.println("long method called: " + a);
}
public static void main(String[] args) {
Test1 t = new Test1();
t.show(10);
}
}
Explanation
10 β int
Available methods:
show(int)
show(long)
Java selects the exact match, which is show(int).
Output
int method called: 10
π Scenario 2
class Test2 {
void show(Integer a){
System.out.println("Integer method called");
}
void show(int a){
System.out.println("int method called");
}
public static void main(String[] args) {
Test2 t = new Test2();
t.show(10);
}
}
Explanation
10 β int
Possible matches:
int β int (Exact Match)
int β Integer (Autoboxing)
Java prefers Exact Match over Autoboxing.
Output
int method called
π Scenario 3
class Test3 {
void show(int a, float b){
System.out.println("int float method");
}
void show(float a, int b){
System.out.println("float int method");
}
public static void main(String[] args) {
Test3 t = new Test3();
t.show(10,10);
}
}
Explanation
show(int,float)
show(float,int)
Both methods require type conversion, so Java cannot decide which is better.
Result
Compile Time Error
Reference to show is ambiguous
π Scenario 4
class Test4 {
void show(int... a){
System.out.println("varargs method");
}
void show(int a){
System.out.println("single int method");
}
public static void main(String[] args) {
Test4 t = new Test4();
t.show(10);
}
}
Explanation
Available methods:
show(int)
show(int...)
Java prefers Exact Match over Varargs.
Output
single int method
π Scenario 5
class Test5 {
void show(int a){
System.out.println("single int method");
}
void show(int... a){
System.out.println("varargs method");
}
public static void main(String[] args) {
Test5 t = new Test5();
t.show();
}
}
Explanation
show(int) β requires argument β
show(int...) β accepts zero arguments β
Output
varargs method
π Scenario 6
class Test6 {
void show(double a){
System.out.println("double method");
}
void show(float a){
System.out.println("float method");
}
public static void main(String[] args) {
Test6 t = new Test6();
t.show(10);
}
}
Explanation
10 β int
Possible widening:
int β float
int β double
Java prefers the smaller widening conversion.
Output
float method
π Scenario 7
class Test7 {
void show(Object o){
System.out.println("Object method");
}
void show(String s){
System.out.println("String method");
}
public static void main(String[] args) {
Test7 t = new Test7();
t.show(null);
}
}
Explanation
null β matches Object and String
But:
String is more specific than Object
Output
String method
π Scenario 8
class Test8 {
void show(String s){
System.out.println("String method");
}
void show(Integer i){
System.out.println("Integer method");
}
public static void main(String[] args) {
Test8 t = new Test8();
t.show(null);
}
}
Explanation
`null β matches both String and Integer
Neither is more specific.`
Result
Compile Time Error
Reference to show is ambiguous
π Scenario 9
class Test9 {
void show(int a,int b){
System.out.println("two int method");
}
void show(int... a){
System.out.println("varargs method");
}
public static void main(String[] args) {
Test9 t = new Test9();
t.show(10,20);
}
}
Explanation
Possible methods:
show(int,int)
show(int...)
Java prefers exact match.
Output
two int method
π Scenario 10
class Test10 {
void show(int a){}
static void show(int a){}
public static void main(String[] args) {
Test10 t = new Test10();
t.show(10);
}
}
Explanation
Java does not allow overloading based only on static vs non-static.
Result
Compile Time Error
method show(int) is already defined
π Scenario 11
class Test11 {
void show(long a){
System.out.println("long method");
}
void show(float a){
System.out.println("float method");
}
public static void main(String[] args) {
new Test11().show(10);
}
}
Explanation
10 β int
Possible widening:
int β long
int β float
Java prefers long conversion.
Output
long method
π Scenario 12
class Test12 {
void show(int a){
System.out.println("single int method");
}
void show(int a,int... b){
System.out.println("int with varargs");
}
public static void main(String[] args) {
new Test12().show(10);
}
}
Explanation
Possible methods:
show(int)
show(int,int...)
Java prefers exact match.
Output
single int method
π Scenario 13
class Test13 {
void show(char c){
System.out.println("char method");
}
void show(int i){
System.out.println("int method");
}
public static void main(String[] args) {
new Test13().show('A');
}
}
Explanation
'A' β char
Exact match exists.
Output
char method
π Scenario 14
class Test14 {
void show(byte b){
System.out.println("byte method");
}
void show(short s){
System.out.println("short method");
}
public static void main(String[] args) {
byte b = 10;
new Test14().show(b);
}
}
Explanation
b β byte
Exact match exists.
Output
byte method
π Scenario 15
class Test15 {
void show(Object o){
System.out.println("Object method");
}
void show(String s){
System.out.println("String method");
}
void show(CharSequence c){
System.out.println("CharSequence method");
}
public static void main(String[] args) {
new Test15().show("Java");
}
}
Explanation
Hierarchy:
Object
β
CharSequence
β
String
Java always chooses the most specific method.
Output
String method
π§ Key Takeaways
β Java always selects the most specific method
β Exact match has highest priority
β Widening happens before autoboxing
β Varargs has lowest priority
β Ambiguous calls cause compile-time errors
π Conclusion
Todayβs learning session helped me understand how Java resolves overloaded methods internally.
These tricky scenarios are very common in Java interviews and real-world development, especially when working with frameworks and libraries.
Mastering these concepts will help me build strong Java fundamentals for Selenium Automation Testing.
π€ A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)