List<? super Number> is a subtype of List<? super Integer>
Ira Go wrote:
Integer is the lower bound - it is at the bottom of allowed types. <? super Number> wildcard bound is a subset of <? super Integer>. Maybe that is why <? super Number> is called a subtype of <? super Integer>
Stephan van Hulst wrote: If List<? super Integer> were a subtype of List<? super Number>, you would be able to assign a List<? super Integer> to a variable of type List<? super Number>. You can't, because it doesn't make sense and would lead to broken code:
If the second statement were allowed, we would be able to add a Float to a List<Integer>.
[2.0]
Mike Simmons wrote:Not really. First, subtype and supertype are well-defined terms in the Java Language Specification, while subset and superset are not. More importantly though, subset and superset don't really apply here.
List<? super Number> is a subtype of List<? super Integer>
Ron McLeod wrote:I believe that he is talking about this kind of scenario
[2.0]
Anil Philip wrote:
List<? super Number> is a subtype of List<? super Integer>
It seems reversed and counterintuitive since Integer is a subtype of Number
Anil Philip wrote:I have assigned a Float to a List<Integer>.
Paul Clapham wrote:
Anil Philip wrote:I have assigned a Float to a List<Integer>.
No... I don't see any List<Integer> anywhere in that code.
[2.0, 5]
Anil Philip wrote:
Ron McLeod wrote:I believe that he is talking about this kind of scenario
I don't think so - it is obvious a Float is not an Integer.
I have assigned a Float to a List<Integer>
Please see my code above (repeated below).
output:
[2.0]
Anil Philip wrote:Sorry, I misspoke - I meant to say list containing integers. Below I add Integer and Float to a List<? super Integer>
I should not be able to do that!
Anil Philip wrote:Okay then, how am I able to do this?
Assigning a Float to a list of Integer.
Anil Philip wrote:It seems reversed and counterintuitive since Integer is a subtype of Number
Anil Philip wrote:Below I add Integer and Float to a List<? super Integer>
I should not be able to do that!
Stephan van Hulst wrote:
Think of a List<? super Integer> as "a list that accepts integers".
Think of a List<? super Number> as "a list that accepts numbers".
A list that accepts numbers is also a list that accepts integers. Therefore, List<? super Number> is a subtype of List<? super Integer>.
Conversely, a list that accepts integers is not a list that accepts numbers, because it cannot accept all different kinds of numbers.
[Janitor{1}, Manager{5}]
Stephan van Hulst wrote:Therefore, List<? super Number> is a subtype of List<? super Integer>.
Anil Philip wrote:Is it correct that:
Anil Philip wrote:But that would be multiple inheritance by classes - which is not allowed by Java.
Mike Simmons wrote:But a class can directly implement more than one interface. ...
Now they also all implement a variety of interfaces, like List, Collection, Iterable, Cloneable, Serializable, and RandomAccess. And they can also have a variety of different generic types. But those are not covered by the rule against multiple inheritance.
Anil Philip wrote:When you invert the inheritance hierarchy, it looks like that is what you get.
John von Neuman wrote:In mathematics, you don't understand things, you just get used to them.
It only looks like it; as MS said, that class only extends one superclass.Anil Philip wrote:. . . a single type multiply inheriting a number of classes . . . it looks like that is what you get.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Stephan van Hulst wrote:
ArrayList<? super Number> is not a class at all. It is a parameterized type.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Stephan van Hulst wrote:A type can not be a subset of another type because types are not sets.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
Stephan van Hulst wrote:A type can not be a subset of another type because types are not sets.
Oh but they are. That is how the JLS defines them. See section 4.5: A class or interface that is generic (§8.1.2, §9.1.2) defines a set of parameterized types.
Mike Simmons wrote:But I believe it refers to the fact that a class or interface can define multiple nested types, in addition to defining a type for itself.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Stephan van Hulst wrote:A type can not be a subset of another type because types are not sets.
Ira Go wrote: <? super Number> wildcard bound is a subset of <? super Integer>.
The Java® Language Specification wrote:A type argument T1 is said to contain another type argument T2, written T2 <= T1, if the set of types denoted by T2 is provably a subset of the set of types denoted by T1 under the reflexive and transitive closure of the following rules (where <: denotes subtyping (§4.10)):
? extends T <= ? extends S if T <: S ? extends T <= ? ? super T <= ? super S if S <: T ? super T <= ? ? super T <= ? extends Object T <= T T <= ? extends T T <= ? super T
Ira Go wrote:Is this referring to
Ira Go wrote:<? super Number> wildcard bound is a subset of <? super Integer>.
Anil Philip wrote:Shouldn't they say subset instead of subtype in both the "hierarchy rules"?
Anil Philip wrote:Hierarchy 2 <T> <<< A<? super T> <<< A<? super S> Example: List<Number> is a subtype of List<? super Number> and List<? super Number> is a subtype of List<? super Integer> Thus, if an overridden method returns List<? super Number>, the overriding method can return List<Number> but not List<Integer> or List<? super Integer>.
It is important to understand that List<Integer> is not a subtype of List<Number> even though Integer is a subtype of Number.
Those are bounded wildcards. They are sets of types.
Example: List<Number> is a subtype of List<? super Number>
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |