Abhinav Abhinav - 3 months ago 12
Java Question

Method overload attempt gives name clash error. Not using Generics

I have the following code.

public class myClass {

public static void myMethod(
ArrayList<ArrayList<Integer>> src,
ArrayList<ArrayList<Integer>> dest,
Integer[] selectedIndices) {

}

public static void myMethod(
ArrayList<ArrayList<Double>> src,
ArrayList<ArrayList<Double>> dest,
Integer[] selectedIndices) {

}
}


I am expecting an overload of
myMethod
but the compiler complains of
name clash
error. Upon searching SO, I found a related (probably, not sure) answer here, but I am not using Generics. So my methods don't go through the type-Erasure. Can someone explain what am I missing?

rD. rD.
Answer

You are missing one important point here. You are still using Generics in your code.

Consider these 2 LOC

  1. ArrayList<ArrayList<Integer>> src
  2. ArrayList<ArrayList<Double>> src

Java internally, in the implementation of these collection classes is using generics only to handle both of these situation and hence you face Type Erasure problem.


EDIT

Just look at this ArrayList class
(It is usually at C:\Program Files\Java\jdk1.7.0_45\src.zip\java\util\ArrayList.java)

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    ...
    ...
    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        size = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    }
    ...
    ...

As you can see the internal structure is using generics as you expected.