Results 1 to 4 of 4
  1. #1
    Lee Rhodes is offline Member
    Join Date
    Jul 2009
    Rep Power

    Default Generics & Inheritance Question

    I have an inheritance structure that forms a tree of classes of Nodes.
    One shared property of these classes is to be able to manage a list of classes
    of type Node or a sub-type of Node.

    Can this be done without creating an "unchecked" exception?

    Java Code:
    public class Node {
        private List<Node> list_ = null;
        private List<? extends Node> listSrc_ = null;
        private List<? super Node> listSink_ = null;
        public <N extends Node> void add(N node) {
            if (list_ == null) {
                list_ = new ArrayList<Node>(); 
                listSrc_  = list_;
                listSink_ = list_;
        @SuppressWarnings("unchecked") //Any way to avoid this?
        public <N extends Node> N get(int n) {
            if ((list_ == null) || (n >= list_.size())) return null;
            return (N)listSrc_.get(n); //causes unchecked warning:
    class Bnode extends Node { // ...}
    class Cnode extends Bnode { // ... }

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Cambridge, UK
    Rep Power


    Don't think so, not in it's current form. listSrc_.get() could return anything that subclasses Node, which is not necessarily compatible with N (e.g., can't cast a JTable to a JPanel, even though both extend JComponent)
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Lee Rhodes is offline Member
    Join Date
    Jul 2009
    Rep Power

    Default Better Form?

    Thanks for the reply. Is there a better form to do this?


  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    Based only on the code shown, I don't see that you are not gaining anything by all the generics, apart from typing your ArrayList as Node. Your add() method always adds a subclass of Node; you don't care what it is. Your get() method does the same, and I don't know what the compiler is doing with N, since the type is erased at compile time, and you don't know what you are returning until run time.

    Try removing all the generic references, apart from List<Node>. Make add() and get() use Node. See if you still get the desired results.

Similar Threads

  1. a question about inheritance
    By litojs in forum New To Java
    Replies: 8
    Last Post: 11-10-2008, 11:07 PM
  2. Generics Question
    By jdgallag in forum New To Java
    Replies: 8
    Last Post: 10-28-2008, 07:15 PM
  3. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 08:58 PM
  4. Question about java generics
    By Arrowx7 in forum New To Java
    Replies: 1
    Last Post: 08-14-2008, 03:37 AM
  5. Generics
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 10:10 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