Method has the same erasure as another method in type

前端 未结 7 847
时光取名叫无心
时光取名叫无心 2020-11-22 05:50

Why is it not legal to have the following two methods in the same class?

class Test{
   void add(Set ii){}
   void add(Set ss){}         


        
7条回答
  •  时光取名叫无心
    2020-11-22 06:34

    This is because Java Generics are implemented with Type Erasure.

    Your methods would be translated, at compile time, to something like:

    Method resolution occurs at compile time and doesn't consider type parameters. (see erickson's answer)

    void add(Set ii);
    void add(Set ss);
    

    Both methods have the same signature without the type parameters, hence the error.

提交回复
热议问题