Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10831

Re: Generics ?

From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: Generics ?
Date 2011-12-17 10:20 -0800
Organization A noiseless patient Spider
Message-ID <jcimia$9dh$1@dont-email.me> (permalink)
References <jcem2m$gbm$1@dont-email.me> <ag5me7h7pph1d4n2b05uvgump03dp7ig0t@4ax.com> <jchb74$n8m$1@dont-email.me> <2eOdnXdWwdSD2HHTnZ2dnUVZ_t6dnZ2d@posted.palinacquisition>

Show all headers | View raw


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<String> extends JList<E> {
     private final DefaultListModel<String> model = new 
DefaultListModel<String>();

     public KList() {
         setModel(model);
         model.addElement("test");
     }
}

C:\com\knutejohnson>javac KList.java
KList.java:3: error: cannot find symbol
public class KList<String> extends JList<E> {
                                          ^
   symbol: class E
KList.java:8: error: method addElement in class DefaultListModel<E> 
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<E> extends JList<E> {
     private final DefaultListModel<E> model = new DefaultListModel<E>();

     public KList() {
         setModel(model);
         model.addElement("test");
     }
}

C:\com\knutejohnson>javac Klist.java
Klist.java:8: error: method addElement in class DefaultListModel<E#2> 
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<String> model = new 
DefaultListModel<String>();

     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<E>) 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<String> {
     private final DefaultListModel<String> model = new 
DefaultListModel<>();

     public KList() {
         setModel(model);
         model.addElement("test");
     }
}

-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-15 21:48 -0800
  Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-15 22:03 -0800
    Re: Generics ? Tassilo Horn <tassilo@member.fsf.org> - 2011-12-16 08:30 +0100
      Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-16 00:08 -0800
  Re: Generics ? Roedy Green <see_website@mindprod.com.invalid> - 2011-12-16 02:10 -0800
    Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-16 22:01 -0800
      Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-16 23:30 -0800
        Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 10:20 -0800
          Re: Generics ? markspace <-@.> - 2011-12-17 10:54 -0800
            Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:25 -0800
              Re: Generics ? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-17 21:20 -0800
                Re: Generics ? "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-12-18 21:08 +1100
                Re: Generics ? Lew <lewbloch@gmail.com> - 2011-12-18 08:17 -0800
                Re: Generics ? markspace <-@.> - 2011-12-18 08:43 -0800
          Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-17 11:33 -0800
            Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:40 -0800
              Re: Generics ? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-17 11:47 -0800
              Re: Generics ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-17 15:13 -0500
      Re: Generics ? Roedy Green <see_website@mindprod.com.invalid> - 2011-12-17 02:46 -0800
  Re: Generics ? "John B. Matthews" <nospam@nospam.invalid> - 2011-12-17 11:18 -0500
    Re: Generics ? Knute Johnson <nospam@knutejohnson.com> - 2011-12-17 11:16 -0800

csiph-web