Consider the following interface :
public interface Addresses <T extends Number> {
T[] getAddress();
void setAddress(T...address);
}
The compiler accepts this implementation:
@Override
public Integer[] getAddress() {.........}
@Override
public void setAddress(Number... address) {.........} //some warning
But does not accept:
@Override
public void setAddress(Integer... address) {.........}
Why can't I override T varargs with Integer varargs, the same way I override T[] with Integer[] ?
How can I override setaddress
so it accepts integers and keep the varargs flexibility ?
Edit: adding the two full classes :
public interface Addresses <T extends Number> {
T[] getAddress();
void setAddress(T...address);//warning: Type safety: Potential heap pollution
}
public class Address implements Addresses{
public Address() {}
@Override
public Integer[] getAddress() {
return null;
}
@Override
public void setAddress(Integer... address) {
// Error: The method setAddress(Integer...) of type Address must
//override or implement a supertype method
}
/*
@Override
public void setAddress(Number... address) {
// No compiler errors
}
*/
}
Edit: is it a duplicate of What is a raw type and why shouldn't we use it? ?
I don't think so. Maybe the information needed to answer my question is included in this post, but I don't think it is the same question.