-1

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.

Community
  • 1
  • 1
c0der
  • 18,467
  • 6
  • 33
  • 65
  • Please show the full definition of the class including how you defined `T` for that class, and the exact warning instead of "some warning". – RealSkeptic Feb 25 '17 at 09:29
  • Trying to replicate your experiment shows the inverse result. The code compiles with `Integer...`, and doesn't compile with `Number...`. Post the whole code of the implementing class. – JB Nizet Feb 25 '17 at 09:30
  • *"Maybe the information needed to answer my question is included in this post"* And that's what defines a dupe. You're here for over 2 years and should know that. – Tom Feb 25 '17 at 13:58
  • 1
    Questions that maybe answered by the same answer, are not necessarily the same questions. If duplicates are "question has been asked before ***AND*** already has an answer" than the question marked as duplicate isn't a duplicate. I agree with " There are similar questions, yes, and so-called “exact” duplicates do happen, but they are kind of rare in my experience." as stated in this post: https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – c0der Mar 01 '17 at 05:17

2 Answers2

1

Assuming you did class NumberAdresses implements Addresses<Number>{ (otherwise i can not reproduce your error).

So why is Integer[] valid as return type for getAdresses(); because you are generally allowed to use supertypes when overriding methods (Thats called covariant return types).

That is not allowed for parameters.

k5_
  • 5,450
  • 2
  • 19
  • 27
1

I found out what's wrong thanks to k5_ answer. Had to use

public class Address implements Addresses<**Integer**>
c0der
  • 18,467
  • 6
  • 33
  • 65