thecoop
thecoop

Reputation: 46158

Correct way to declare a method with two type parameters in inheritance hierarchy

When writing a method that takes two objects, with two type parameters in a subtype-supertype relation, what is the best way of declaring your intentions out of these options?

  1. Declare both super and extends:

    public static <T> void copy(List<? super T> dst, List<? extends T> src) { ... }
    
  2. Declare just the extends:

    public static <T> void copy(List<T> dst, List<? extends T> src) { ... }
    
  3. Declare just the super:

    public static <T> void copy(List<? super T> dst, List<T> src) { ... }
    

From my understanding, all three are correct, and are equivalent to each other, as all you're interested in is the relative inheritance of the type arguments of dst and src. So which is better?

Upvotes: 0

Views: 88

Answers (1)

softarn
softarn

Reputation: 5496

I think extends is most common and if there is no need to use both you shouldn't. So I'd go with extends only.

So there is not a correct way, unless you have a convention that specifies it.

Upvotes: 1

Related Questions