sanbhat
sanbhat

Reputation: 17622

Java method overload - ambiguity

While I was doing some runs to test some code in this thread I found out a strange thing, If you consider the following program

import java.util.ArrayList;
import java.util.List;

public class OverloadTest {

    public String test1(List l){
        return "abc";
    }

    public int test1(List<Integer> l){
        return 1;
    }

    public static void main(String [] args) {
        List l = new ArrayList();
        System.out.println(new OverloadTest().test1(l));
    }
}

I was expecting Java compiler to show ambiguity error due to byte-code Erasure property, but it didn't. Now when I tried to run this code, I was expecting that test1(List) will be called and the output would be "abc" but to my surprise it called test1(List<Integer>) (output was 1)

I even tried like below

List l = new ArrayList();
l.add("a");
System.out.println(new OverloadTest().test1(l));

But still found Java calling test1(List<Integer> param) method and when i inspected the param it had the String "a" ( how did Java cast List<String> to List<Integer> ?)

Upvotes: 8

Views: 1194

Answers (1)

durron597
durron597

Reputation: 32323

This is a fixed bug. https://bugs.eclipse.org/bugs/show_bug.cgi?id=354229

It looks like this bug existed in javac5, javac6 and ecj for Eclipse 3.7, but it was fixed in Eclipse 3.8 and later.

Upvotes: 4

Related Questions