Because the compiler doesn't know what type of argument driver. Drive(? vehicle) will accept. Should it take a Chevrolet, a Honda, or some other type?
Because the compiler doesn't know what type of argument driver. Drive(? vehicle) will accept. Should it take a Chevrolet, a Honda, or some other type?
For more details you might find Gilad Bracha's Generics Tutorial helpful. What is the purpose of your drive() method. Does the vehicle parameter need to be typed to the specific subtype of Car?
Would it be more appropriate for it to simply accept a Car? The following was part of my original answer, but discussion in the comments proved that it was incorrect. I'm leaving it here so the comments aren't orphaned.
Try declaring your method like this: public void drive(T vehicle) and see if that works better for you.
1 I've fixed it in my edit. – ChrisH Jul 12 '10 at 15:36 I still fail to see how this answers the question. You still can't do driver.
Drive(aChevrolet), can you? – polygenelubricants Jul 12 '10 at 15:49 You are right, assuming that driver is still declared as a Driver, however, since class Driver requires its generics argument to be Car or a subclass of Car, it would be reasonable to declare driver as type Driver without any loss of flexibility. With that additional change I think my answer works.
– ChrisH Jul 12 '10 at 17:32 No, a Driver may be a Driver, and since Chevrolet does not extend BMW (I hope I got that right, I don't know cars), you still can't drive(aChevrolet). – polygenelubricants Jul 13 '10 at 9:10 @polygenelubricants: Yes, you are right. I'm going to edit my post again.
– ChrisH Jul 13 '10 at 19:31.
It depends on the kind of wildcard. Using an object through a reference variable of a wildcard parameterized type is restricted. Consider the following class: Example (of a generic class): class Box { private T t; public Box(T t) { this.
T = t; } public void put(T t) { this. T = t;} public T take() { return t; } public boolean equalTo(Box other) { return this.t. Equals(other.
T); } public Box copy() { return new Box(t); } } When we use a reference variable of a wildcard instantiation of type Box to access methods and fields of the referenced object the compiler would reject certain invocations. Example (of access through a wildcard parameterized type): class Test { public static void main(String args) { Box box = new Box("abc"); box. Put("xyz"); // error box.
Put(null); // ok String s = box.take(); // error Object o = box.take(); // ok boolean equal = box. EqualTo(box); // error equal = box. EqualTo(new Box("abc")); // error Box box1 = box.copy(); // ok Box box2 = box.copy(); // error } } Essentially?
Has less information for the generic type system, and thus to enforce typesafety certain invokations must be rejected, because they're not typesafe. A Box may be a Box, a Box, or even a Box. Thus given a Box box, box.
Put("xyz") must be rejected. References Angelika Langer's Java Generics FAQ Which methods and fields are accessible/inaccessible through a reference variable of a wildcard parameterized type? Related questions How can I add to List data structures?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.