import java.util.*;
class A extends HashSet<Integer> {
public boolean add(Object obj){ //compiler error
return true;
}
}
or
class Abc <T> {
public void add(T t){} //compiler error
public void add(Object i){} //compiler error (can't overload?)
}
Error:Name clash: The method add(Object) of type test2 has the same erasure as add(E) of type HashSet but does not override it
i do not know what is the concept behind above error can any one suggest where i can study this concept?
The concept at work here is called type erasure.
HashSetdefines a methodadd(T), and you define a methodadd(Object). At a glance one might think this is OK; that your method just overloadsadd. However, the erasure ofTisObjectand so the two have the same erased signature.Now, that would be fine if your method properly overrode the method from
HashSet. But to do so you should be usingadd(Integer)and notadd(Object). You’re not properly overriding the parent method, so instead it is reported as a conflict since a class cannot provide two methods with the same signature.Your
Abcexample follows the same reasoning. The two methods you declared have the same erased signature so they clash.Further Reading
Angelika Langer Generics FAQ