1

Consider this hypothetical class (which I found in a online video):

public class Contrived<T extends Number> extends ArrayList<T> {
      List <? extends T> values;
      ......
    }

Here the type variables that Contrived can accept is Number or some sub-type of Number. But the class itself inherits from ArrayList, so what-ever type the class gets, will determine the type of ArrayList.

Now there is a field called values, which is List<? extends T values>. so what does this mean here? Does the list can hold anything that extends T (which in turn extend Number).

by PECS (producer extends, consumer super) rule, can I only use this List to read elements but not add to the list.

how will the constructor look like, if I need to pass a list of doubles or some type T?

brain storm
  • 30,124
  • 69
  • 225
  • 393
  • 1
    I don't think I fully got the idea behind the `Contrived` class. What is it supposed to do? It extends `ArrayList`, and has a list `values` as a reference variable. The class thus basically contains two lists, what's the intention of the class? A useful constructor depends on that (e.g. "What should the constructor do?"). Can you link to the source? – dst Feb 13 '14 at 00:17
  • @dst: it is a hypothetical class. nothing important it is doing as such. but I am wondering how a constructor for it will look it like and what operations can be done the given `list` field? – brain storm Feb 13 '14 at 00:19
  • `public Contrived(List list)` ? – Federico Berasategui Feb 13 '14 at 00:20
  • @HighCore @user1988876 or `public Contrived(List extends T> list)`? Or do you want a method body? – dst Feb 13 '14 at 00:21
  • @HighCore: shouldn't constructor be `public Contrived(List extends T> list)` – brain storm Feb 13 '14 at 00:22
  • You can only assign something to the list so that's all the constructor can do. I don't totally understand what the question is here. If the class is hypothetical, how would we know what it's supposed to do? – Radiodef Feb 13 '14 at 00:22
  • @user1988876 it depends on which list you wish to fill (the array list or the instance variable), or if you wish to do something completely different – dst Feb 13 '14 at 00:22

1 Answers1

2

You can have a constructor that takes a List<T> or a List<? extends T>. Continuing to contrive classes:

class B extends Number{
   public double doubleValue() { return 0; }
   public float floatValue() { return 0; }
   public long longValue() { return 0; }
   public int intValue() { return 0; }
}
class C extends B{}

I add a contrived, legal constructor to the Contrived class, taking a List<? extends T>:

public Contrived(List<? extends T> values)
{
   this.values = values;
}

This allows me to write a main method such as the following:

public static void main(String[] args)
{
   List<C> bList = Arrays.asList(new C(), new C(), new C());
   Contrived<B> ci = new Contrived<B>(bList);
}

I can pass a List<C> into the constructor for a Contrived<B>.

Another design for the contrived constructor that is legal for Contrived could be, taking a List<T>:

public Contrived(List<T> values)
{
   this.values = values;
}

Such a constructor doesn't allow a List<C> for a Contrived<B>, because now the types must match, as in the following two Contrived examples:

public static void main(String[] args)
{
   List<C> cList = Arrays.asList(new C(), new C(), new C());
   Contrived<C> ci = new Contrived<C>(cList);  // must match now
}

And

public static void main(String[] args)
{
   List<B> bList = Arrays.asList(new B(), new B(), new B());
   Contrived<B> ci = new Contrived<B>(bList);  // must match now
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • 1
    As long as we are talking contrived, you can also assign `this` to `values`. – Radiodef Feb 13 '14 at 00:24
  • @Radiodef Confirmed, your contrived example, `this.values = this;`, is legal inside either of my contrived `Contrived` constructors. – rgettman Feb 13 '14 at 00:27
  • could be have both constructors in case of overloading;but that would not make sense in this case I suppose; since the first one is a relaxed version of the latter restricted one..? – brain storm Feb 13 '14 at 00:29
  • 1
    @user1988876 Well actually you can't have both since they have the same erasure. They are just two different examples. – Radiodef Feb 13 '14 at 00:30