0

I have a method:

public static void test(ArrayList<Object> list)

However, when I try to call:

test(new ArrayList<String>)

the compiler throws errors indicating no such method.

But the strange thing is, if I define another method:

public static void test(ArrayList<String> list)

compilers throws another error indicating the two methods share same signature. After this I even try:

if(new ArrayList<Object>().getClass().equals(new ArrayList<Resource>().getClass())){
            System.out.println("same");
        }
        else {
        }

The result indicates the two classes are the same.

Can someone explain it a little bit about the generic type for arraylist?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Qing
  • 1,401
  • 2
  • 21
  • 41
  • *" indicating the two methods share same signature."* You shouldn't cut out important information. Your compiler surely says that they have the same signature _after type erasure_. If you don't know what type erasure is, then google it, because it is important here. – Tom Feb 03 '16 at 09:20
  • It's because `ArrayList` can't be converted to `ArrayList`. You can use `?` like this `public static void test(ArrayList> list)`. – user2004685 Feb 03 '16 at 09:24
  • You can always use a pretty simple genetic method. Here is the code: `public static void test(ArrayList list)`. Now you can call both `test(new ArrayList)` and `test(new ArrayList)` and the same function will be invoked. – mehrdadjg Feb 03 '16 at 09:28

0 Answers0