Generics Compile Problem

Discussion in 'Java' started by petek1976, Jan 11, 2007.

  1. petek1976

    petek1976 Guest

    Can anyone tell me why this code will not compile? I'm only including
    the relevant portions of the code. I don't understand the error from
    the compiler:

    public class Msg
    {
    }

    public interface MsgListener<T>
    {
    public void Process(T msg);
    }

    import java.util.*;
    public class MsgRegistration
    {
    private HashMap<String,MsgListener<? extends Msg>> mcMap = new
    HashMap<String,MsgListener<? extends Msg>>();

    public <X extends Msg> void doit(String acName, X acMsg)
    {
    MsgListener<? extends Msg> lcListener= mcMap.get(acName);
    lcListener.Process(acMsg);
    }

    }

    MsgRegistration.java:10: Process(capture of ? extends Msg) in
    MsgListener<capture of ? extends Msg> cannot be applied to (X)
    lcListener.Process(acMsg);

    Why can't it be applied to X since X extends Msg?
    petek1976, Jan 11, 2007
    #1
    1. Advertising

  2. petek1976

    Daniel Pitts Guest

    petek1976 wrote:
    > Can anyone tell me why this code will not compile? I'm only including
    > the relevant portions of the code. I don't understand the error from
    > the compiler:
    >
    > public class Msg
    > {
    > }
    >
    > public interface MsgListener<T>
    > {
    > public void Process(T msg);
    > }
    >
    > import java.util.*;
    > public class MsgRegistration
    > {
    > private HashMap<String,MsgListener<? extends Msg>> mcMap = new
    > HashMap<String,MsgListener<? extends Msg>>();
    >
    > public <X extends Msg> void doit(String acName, X acMsg)
    > {
    > MsgListener<? extends Msg> lcListener= mcMap.get(acName);
    > lcListener.Process(acMsg);
    > }
    >
    > }
    >
    > MsgRegistration.java:10: Process(capture of ? extends Msg) in
    > MsgListener<capture of ? extends Msg> cannot be applied to (X)
    > lcListener.Process(acMsg);
    >
    > Why can't it be applied to X since X extends Msg?


    The problem is that "lcListener" is of type "? extends Msg", which
    might mean its a "Y" which extends Msg. X objects wouldn't be valid in
    that case...
    What you want is MsgListener<Msg>
    Actually, it looks like you don't even want generics in this case. if X
    is a subclass of Msg, you can just use polymorphism.
    Daniel Pitts, Jan 11, 2007
    #2
    1. Advertising

  3. petek1976

    Lew Guest

    petek1976 wrote:
    >> public interface MsgListener<T>
    >> {
    >> public void Process(T msg);
    >> }


    Method and variable names should start with a lower-case letter for readability.

    - Lew
    Lew, Jan 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    5,970
    John C. Bollinger
    May 20, 2005
  2. Oliver Wong
    Replies:
    5
    Views:
    433
    Oliver Wong
    Nov 2, 2005
  3. Mitch
    Replies:
    6
    Views:
    861
    rySource
    Mar 18, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    840
    Lionel B
    Mar 1, 2007
  5. Soul
    Replies:
    0
    Views:
    505
Loading...

Share This Page