DEV Community

Cover image for πŸš€ Day 19 of My Automation Journey – Method Overloading Tricky Questions
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 19 of My Automation Journey – Method Overloading Tricky Questions

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

10 is an int literal, so Java finds the exact match.

Output

int method called
Enter fullscreen mode Exit fullscreen mode

🧠 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)`               |
Enter fullscreen mode Exit fullscreen mode

πŸ“Š 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)    |
Enter fullscreen mode Exit fullscreen mode

⚠️ 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     |
Enter fullscreen mode Exit fullscreen mode

βœ… One-line Memory Trick

`Exact Match β†’ Widening β†’ Autoboxing β†’ Varargs`
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
10 β†’ int

Available methods:

show(int)
show(long)

Java selects the exact match, which is show(int).

Output

int method called: 10
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
10 β†’ int

Possible matches:

int β†’ int (Exact Match)
int β†’ Integer (Autoboxing)

Java prefers Exact Match over Autoboxing.

Output

int method called
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

Available methods:

show(int)
show(int...)

Java prefers Exact Match over Varargs.

Output

single int method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
show(int) β†’ requires argument ❌
show(int...) β†’ accepts zero arguments βœ”

Output

varargs method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
10 β†’ int

Possible widening:

int β†’ float
int β†’ double

Java prefers the smaller widening conversion.

Output

float method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
null β†’ matches Object and String

But:

String is more specific than Object
Output

String method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

Possible methods:

show(int,int)
show(int...)

Java prefers exact match.

Output

two int method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);
    }


}
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
10 β†’ int

Possible widening:

int β†’ long
int β†’ float

Java prefers long conversion.

Output

long method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

Possible methods:

show(int)
show(int,int...)

Java prefers exact match.

Output

single int method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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');
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
'A' β†’ char

Exact match exists.

Output

char method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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);

    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
b β†’ byte

Exact match exists.

Output

byte method
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 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");
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

Hierarchy:

Object
↑
CharSequence
↑
String

Java always chooses the most specific method.

Output

String method
Enter fullscreen mode Exit fullscreen mode

🧠 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)