See also:
A digression. Feel free to skip.
For programmers who are also familiar with C#, a similar feature exists:
The terms, covariance and contravariance, can be traced back to the early discussions coming from the Liskov substitution principle (LSP).
The design goal of lower-bounded wildcard (LBWC) is to ensure the type safety of the following piece of code.
(This is just one example; one of the very simple example among others.)
List<? super RationalNumber> containerOne = new ArrayList<RationalNumber>();
List<? super RationalNumber> containerTwo = new ArrayList<Number>();
List<? super RationalNumber> containerThree = new ArrayList<Object>();
RationalNumber itemValue = new RationalNumber(22, 7);
containerOne.add(itemValue);
containerTwo.add(itemValue);
containerThree.add(itemValue);
In short, LBWC allows the "handles" - generic classes having an LBWC (containerOne
, containerTwo
, containerThree
in the above sample) to:
- Be assigned instances of the generic class (of type parameter
T
) whenever it is known that RationalNumber
can be safely up-cast to T
.
Now, we use this knowledge and try to answer the question: can we pass in a Number
to such a "handle" - a generic class having an LBWC of RationalNumber
?
Given that
- It is valid to assign an instance of
new List<RationalNumber>()
to a handle of List<? super RationalNumber> varGeneric
.
- Of course it is valid to do something entirely different; but the design of LBWC is that this is the situation that it needs to deal with.
We obtain this corollary question:
- Can we call
new List<RationalNumber>().add(new Number(...))
?
Seeing that it is LBWC's design goal to prevent this from happening, one can derive this conclusion:
- If LBWC is designed to be enforceable at all (by the compiler, or language police), it must prevent this assignment (passing in a
Number
for a type ?
for which the LBWC is RationalNumber
) from happening.
This answers the question from a design perspective. The matter of enforcement can only be answered by senior members of the community who are familiar with the construction of the Java language and its compiler tools. Comments from the general crowd will only provide speculations, or "hypotheses" or "theories".