Results 1 to 2 of 2
  1. #1
    Arrowx7 is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default Question about java generics

    Hello, here is the code that's throwing the compile error:

    Java Code:
    List<Document<TextType>> doclist = ......
    DocumentRegistry registry = new DocumentRegistry(doclist);

    Definition:
    Java Code:
    public DocumentRegistry(List<Document<?>> documents) {
    
    }

    Error:
    Java Code:
     cannot find symbol
    symbol  : constructor DocumentRegistry(java.util.List<org.myproj.Document<org.myproj.doctypes.TextType>>)
    if I change the constructor to
    public DocumentRegistry(List<Document<TextType>> documents)
    it works, but I want to pass in any document object. I thought the wildcard ? would include TextType;
    FYI, Document class is a wrapper class.
    and the DocumentRegistry extends HashMap for some reason if I remove "extends HashMap" it works! Why is that??

    Any help would be appreciated thanks!
    Last edited by Arrowx7; 08-12-2008 at 10:02 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public DocumentRegistry(List<Document<?>> documents)
    This suggests that I could pass anything, eg, Point, to your Document and it would be okay.
    How about if you specified an extension of TextType or a superclass of TextType?
    Java Code:
    public DocumentRegistry(List<Document<? extends TextType>> documents)
    // or
    public DocumentRegistry(List<Document<? extends SuperClassOfTextType>> documents)

Similar Threads

  1. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-10-2008, 11:08 PM
  2. Java confused over Generics?
    By Bibendum in forum New To Java
    Replies: 3
    Last Post: 12-26-2007, 06:23 AM
  3. Java Generics (an introduction)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 11-27-2007, 06:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •