Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: Generics ? Date: Sat, 17 Dec 2011 10:20:58 -0800 Organization: A noiseless patient Spider Lines: 129 Message-ID: References: <2eOdnXdWwdSD2HHTnZ2dnUVZ_t6dnZ2d@posted.palinacquisition> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 17 Dec 2011 18:20:58 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="9649"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ExPgByvNB7k5ZYyfFzLLv" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <2eOdnXdWwdSD2HHTnZ2dnUVZ_t6dnZ2d@posted.palinacquisition> Cancel-Lock: sha1:4EnyUOoKouCSNybtsAFihmX9G6k= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10831 On 12/16/2011 11:30 PM, Peter Duniho wrote: > On 12/16/11 10:01 PM, Knute Johnson wrote: >> Yeah, unfortunately that doesn't work. Is it possible to extend JList >> and not get unchecked warnings? > > Please be more specific. In what way does it "not work"? Roedy's > suggestion is basically an exact copy of mine, and is correct, provided > you use the suggestion correctly. > > Since you didn't post a proper code example showing what you tried, > there's no way to know for sure why it didn't work. But we can say for > sure that what you tried did not match either of the code examples I > provided in my first reply (not counting my goof of using "int" instead > of "Integer" as an example type parameter), since the techniques shown > in those examples _do_ work. > > Pete Example #1 import javax.swing.*; public class KList extends JList { private final DefaultListModel model = new DefaultListModel(); public KList() { setModel(model); model.addElement("test"); } } C:\com\knutejohnson>javac KList.java KList.java:3: error: cannot find symbol public class KList extends JList { ^ symbol: class E KList.java:8: error: method addElement in class DefaultListModel cannot be ap plied to given types; model.addElement("test"); ^ required: String found: java.lang.String reason: actual argument java.lang.String cannot be converted to String by meth od invocation conversion where String,E are type-variables: String extends Object declared in class KList E extends Object declared in class DefaultListModel 2 errors Example #2 Roedy's second example that he thought should work import javax.swing.*; public class KList extends JList { private final DefaultListModel model = new DefaultListModel(); public KList() { setModel(model); model.addElement("test"); } } C:\com\knutejohnson>javac Klist.java Klist.java:8: error: method addElement in class DefaultListModel cannot be applied to given types; model.addElement("test"); ^ required: E#1 found: String reason: actual argument String cannot be converted to E#1 by method invocation conversion where E#1,E#2 are type-variables: E#1 extends Object declared in class KList E#2 extends Object declared in class DefaultListModel 1 error Example #3 only gives a warning import javax.swing.*; public class KList extends JList { private final DefaultListModel model = new DefaultListModel(); public KList() { setModel(model); model.addElement("test"); } } C:\com\knutejohnson>javac KList.java Note: KList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. C:\com\knutejohnson>javac -Xlint:unchecked KList.java KList.java:7: warning: [unchecked] unchecked call to setModel(ListModel) as a member of the raw type JList setModel(model); ^ where E is a type-variable: E extends Object declared in class JList 1 warning Example #4 Roedy's first suggestion compiles without warning but it is no longer generic. Which brings me back to my real question, can you extend a generic class and still be generic? import javax.swing.*; public class KList extends JList { private final DefaultListModel model = new DefaultListModel<>(); public KList() { setModel(model); model.addElement("test"); } } -- Knute Johnson