Adam Chalkley wrote:
but how come I don't get an error in the team class if I pass FootballPlayer as the type parameters Team<FootballPlayer>??
What type of argument does it accept? By looking at its signature, you can see that it accepts a single argument whose type is Box<Number>. But what does that mean? Are you allowed to pass in Box<Integer> or Box<Double>, as you might expect? The answer is "no", because Box<Integer> and Box<Double> are not subtypes of Box<Number>.
Adam Chalkley wrote:I still can't figure out the difference between these two instances
is legal
Adam Chalkley wrote:
and this is not when you pass lets just say Box<Double> into the method the thing I don't get is why is the first snippet of code valid aren't you basically doing the same thing? in the first snippet you can do box.add(new Double(10.1)); because has a is a relationship with number and in the second snippet isn't this the same thing because Double is a number
Adam Chalkley wrote:hey Henry thanks for the reply I understand that but I don't understand the difference between the first snippet and second snippet of code
Stephan has shown how allowing a Box<Double> to be a subtype of Box<Number> is like trying to sell the compiler a pig in a poke. There used to be a section in the Java™ Tutorials showing the relation between Cages, with Cage<Animal>, Cage<Butterfly> and Case<Lion> examples, similar to Henry's box of puppies. I think you would have to look for old versions of the Java™ Tutorials to find it. Sometimes the same explanation is clearer if you read it in several places.Stephan van Hulst wrote:. . . if you have a method that accepts a Box<Number>, you can NOT pass it a Box<Double>. . . .
Adam Chalkley wrote:and why would
give a compilation error?
Adam Chalkley wrote:
why is Box<? extends Number> the super type of of box<Number> and box<Double>?
Adam Chalkley wrote:
Adam Chalkley wrote:
Adam Chalkley wrote:
Adam Chalkley wrote:
I understand the first part that this method takes a list that implement the interface Comparable,but what does the <? super T> after Comparable mean?
I presume you mean this method. Let's start by having another look at Comparable. Note it is calledAdam Chalkley wrote: . . . binarySearch(List<? extends Comparable<? super T>> list, T key) . . .
at the top of that page. So let's try working out a T or two:-You can have a Fahrenheit class, a Réaumur class etc in that package, but no non‑temperature classes. So the Temperature class cannot be subclassed outside the package, and the other classes being final can't be subclassed at all. Now let's have a List<Temperature> containing objects of different scales. Now, temperatures do implement a total ordering; you can always say that warmer == more, and you can also say that 20℃ is the same as 68℉. You don't want each subclass of Temperature to implement compareTo; indeed you can't because I marked it final. But each subclass can use the superclass' method because each can calculate a °K value.Interface Comparable<T>
Watch this space.
Adam Chalkley wrote:Hey Campbell thanks for the detailed post much appreciated,I have a better grasp on it now but lets say in the example you used and in the BinarySearch Method
binarySearch(List<? extends Comparable<? super T>> list, T key) .
Adam Chalkley wrote:
you use T,could we replace the question mark for a T and would it work the same ,what would be the difference?
List<? extends Shape> is an example of a bounded wildcard. The ? stands for an unknown type, just like the wildcards we saw earlier. However, in this case, we know that this unknown type is in fact a subtype of Shape. (Note: It could be Shape itself, or some subclass; it need not literally extend Shape.) We say that Shape is the upper bound of the wildcard.
There is, as usual, a price to be paid for the flexibility of using wildcards. That price is that it is now illegal to write into shapes in the body of the method. For instance, this is not allowed:
You should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape. Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.
you should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape. Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype,
Adam Chalkley wrote:
I don't understand what they mean by this line in particular and why this would be illegal
you should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape. Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype,
Adam Chalkley wrote:I don't understand what they mean by Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.
Adam Chalkley wrote:
also why wouldn't this code work?
All things are lawful, but not all things are profitable.
Adam Chalkley wrote:
also why wouldn't this code work?
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
Remember that the compiler is dumb. It doesn't know that all Java classes extend Object. (This is my guess, as I'm not an expert on compilers.)
Consider Paul's rocket mass heater. |