Java - Lower-Bounded Wildcards

Introduction

The syntax for using a lower-bound wildcard is

<? super T>

which means "anything that is a supertype of T."

Example

The following copy() method with lower-bounded wildcard requires the source and the dest arguments be of the same type.

The dest argument of the copy() method could be either T, same as source, or any of its supertype.

You can use the copy() method to copy the contents of a Wrapper<String> to a Wrapper<Object>.

Since Object is the supertype of String.

You cannot use it to copy from an Object type wrapper to a String type wrapper.

Demo

public class Main {
  public static void main(String[] args) {
    Wrapper<Object> objectWrapper = new Wrapper<Object>(new Object());
    Wrapper<String> stringWrapper = new Wrapper<String>("Hello");
    copy(stringWrapper, objectWrapper); 
    System.out.println(objectWrapper.get());
  }/*from   w ww .  j  av a 2s.  co m*/
  //Lower-Bounded Wildcards
  public static <T> void copy(Wrapper<T> source, Wrapper<? super T> dest){
    T value = source.get();
    dest.set(value);
  }

}

class Wrapper<T> {
  private T ref;

  public Wrapper(T ref) {
    this.ref = ref;
  }

  public T get() {
    return ref;
  }

  public void set(T a) {
    this.ref = a;
  }
}

Result