DEV Community

Kesavarthini
Kesavarthini

Posted on

Method Overloading Tasks

Method Overloading Tricky Questions :

class Test {
    void show(int a) { }
    void show(long a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a)
10 is int → exact match is preferred over widening (int → long)

class Test {
    void show(Integer a) { }
    void show(int a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a)
Exact primitive match is preferred over autoboxing (int → Integer)

class Test {
    void show(int a, float b) { }
    void show(float a, int b) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10, 10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: Compile-time error (Ambiguous)
Both require one widening conversion → compiler confused

class Test {
    void show(int... a) { }
    void show(int a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a)
Normal method > varargs

class Test {
    void show(int a) { }
    void show(int... a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show();
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int... a)
Only varargs can take zero arguments

class Test {
    void show(double a) { }
    void show(float a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(double a)
10 is int → prefers wider type with better precision match → double

class Test {
    void show(Object o) { }
    void show(String s) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(null);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(String s)
More specific type is chosen (String < Object)

class Test {
    void show(String s) { }
    void show(Integer i) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(null);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: Compile-time error (Ambiguous)
Both are unrelated → compiler can’t decide

class Test {
    void show(int a, int b) { }
    void show(int... a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10,20);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a, int b)
Fixed argument method > varargs

class Test {
    void show(int a) { }
    static void show(int a) { }

    public static void main(String[] args) {
        Test t = new Test();
        t.show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: Compile time error
Static/non-static doesn’t matter → same signature

class Test {
    void show(long a) { }
    void show(float a) { }

    public static void main(String[] args) {
        new Test().show(10);
    }
}

Enter fullscreen mode Exit fullscreen mode

ANSWER: Compile time error(Ambiguous)
int → long and int → float both valid → confusion

class Test {
    void show(int a) { }
    void show(int a, int... b) { }

    public static void main(String[] args) {
        new Test().show(10);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(int a)
Exact match beats varargs

class Test {
    void show(char c) { }
    void show(int i) { }

    public static void main(String[] args) {
        new Test().show('A');
    }
}

Enter fullscreen mode Exit fullscreen mode

ANSWER: show(char c)
Exact match (char) preferred over widening (char → int)

class Test {
    void show(byte b) { }
    void show(short s) { }

    public static void main(String[] args) {
        byte b = 10;
        new Test().show(b);
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(byte b)

class Test {
    void show(Object o) { }
    void show(String s) { }
    void show(CharSequence c) { }

    public static void main(String[] args) {
        new Test().show("Java");
    }
}
Enter fullscreen mode Exit fullscreen mode

ANSWER: show(String s)

Top comments (0)