Bug 72857 - 1.5 generics don't fully support polymorphism
Summary: 1.5 generics don't fully support polymorphism
Status: RESOLVED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1 M2   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-08-30 03:17 EDT by Alan Malloy CLA
Modified: 2004-12-03 12:25 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alan Malloy CLA 2004-08-30 03:17:04 EDT
There appears to be a problem with declaring a variable of some generic type 
and initializing it to a subclass of that type, which should work fine. (Note 
that this may be a duplicate of bug #72754) Below is an example:

Map<String, Set<String>> m = new TreeMap<String, TreeSet<String>>();
Then faced with this code, the Eclipse compiler complains: "Type mismatch: 
cannot convert from TreeMap<String,TreeSet<String>> to 
Map<String,Set<String>>".

I suspect that this may be a duplicate of bug #72754 because a declaration 
without nested generics, such as Map<String, String>, works just fine. But 
since it may not be (this has to do with variable assignment, not method 
invocation), I felt I should bring it to your attention. Thank you.
Comment 1 Kent Johnson CLA 2004-08-30 11:22:37 EDT
You cannot pass in a TreeSet since it expects a Set. Read up on subtypes & 
generics.

java version "1.5.0-rc", build 1.5.0-rc-b63 complains with:

test.java:5: incompatible types
found   : 
java.util.TreeMap<java.lang.String,java.util.TreeSet<java.lang.String>
>
required: java.util.Map<java.lang.String,java.util.Set<java.lang.String>>
                Map<String, Set<String>> m = new TreeMap<String, 
TreeSet<String>
>();
                                             ^
1 error
Comment 2 John Maraist CLA 2004-12-03 12:25:30 EST
A workaround for the original poster would be to declare 
 
Map<String, ? extends Set<String>> m