Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Let's test your understanding of generics. Is the following code snippet legal?Line 1 is certainly legal. The trickier part of the question is line 2. This boils down to the question: is aList<String> ls = new ArrayList<String>(); // 1 List<Object> lo = ls; // 2List
ofString
aList
ofObject
. Most people instinctively answer, "Sure!"Well, take a look at the next few lines:
Here we've aliasedlo.add(new Object()); // 3 String s = ls.get(0); // 4: Attempts to assign an Object to a String!ls
andlo
. Accessingls
, a list ofString
, through the aliaslo
, we can insert arbitrary objects into it. As a resultls
does not hold justString
s anymore, and when we try and get something out of it, we get a rude surprise.The Java compiler will prevent this from happening of course. Line 2 will cause a compile time error.
In general, if
Foo
is a subtype (subclass or subinterface) ofBar
, andG
is some generic type declaration, it is not the case thatG<Foo>
is a subtype ofG<Bar>
. This is probably the hardest thing you need to learn about generics, because it goes against our deeply held intuitions.We should not assume that collections don't change. Our instinct may lead us to think of these things as immutable.
For example, if the department of motor vehicles supplies a list of drivers to the census bureau, this seems reasonable. We think that a
List<Driver>
is aList<Person>
, assuming thatDriver
is a subtype ofPerson
. In fact, what is being passed is a copy of the registry of drivers. Otherwise, the census bureau could add new people who are not drivers into the list, corrupting the DMV's records.To cope with this sort of situation, it's useful to consider more flexible generic types. The rules we've seen so far are quite restrictive.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.