Method has the same erasure as another method in type

前端 未结 7 849
时光取名叫无心
时光取名叫无心 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:28

    Java generics uses type erasure. The bit in the angle brackets ( and ) gets removed, so you'd end up with two methods that have an identical signature (the add(Set) you see in the error). That's not allowed because the runtime wouldn't know which to use for each case.

    If Java ever gets reified generics, then you could do this, but that's probably unlikely now.

提交回复
热议问题