Results 1 to 4 of 4
  1. #1
    shaboinkin is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Quick question about generics

    My professor sent us some code that he used to create a node. We then need to create a tester for it and I'm trying to understand one line he gave us.

    PHP Code:
    class Node<E extends Comparable<E>>
    Could someone tell me what is going on here? I looked up Comparable on the api but I'm still somewhat fuzzy on why it's used here.


    Thanks
    -shaboinkin

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This means that when you declare a Node variable and when you initialize a Node object, it must use a generic type that extends Comparable. So if you have two classes, Foo and Bar like so:

    Java Code:
    public class Foo implements Comparable<Foo> {
      
      public int compareTo(foo otherFoo) {
         /// etc...
      }
      
    }

    Java Code:
    public class Bar {
    
    }

    You can use Foo as the generic Type of your Node variable/object but not Bar since Bar does not implement Comparable<Bar>. So this is OK:

    Java Code:
    Note<Foo> myNode = new Node<Foo>();


    but this won't compile:
    Java Code:
    Note<Bar> myNode = new Node<Bar>();

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm guessing the node is part of some ordered structure like a tree. A binary tree might organise its nodes so that the left child was "less than" the right child, thereby keeping all the nodes in a way that can be iterated over easily.

    But this depends on the node's data being of a particular type: they must be things that implement Comparable, ie have a compareTo() method. For example Strings are like this.

    public final class String
    extends Object
    implements Serializable, Comparable<String>, CharSequence

    Other classes are not comparable like this. (For instance StringBuilder doesn't define any way to compare one instance with another.)

    So a structure that wants to take advantage of the "self comparability" of the data stored in the nodes can be built for String instances, but not for StringBuilder instances. And that is what the declaration is expressing:

    class Node<E extends Comparable<E>>

    "A Node of Elements that implement the comparable interface with respect to themselves". Note "extends" is used here although both implementing and extending some super type are allowed.

    See also Bounded Type Parameters in Oracle's Tutorial.

  4. #4
    shaboinkin is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Generics Question
    By erversteeg in forum New To Java
    Replies: 3
    Last Post: 02-11-2011, 05:13 AM
  2. Question regarding generics
    By Leaflord in forum Advanced Java
    Replies: 9
    Last Post: 08-29-2009, 02:24 PM
  3. Generics & Inheritance Question
    By Lee Rhodes in forum Advanced Java
    Replies: 3
    Last Post: 07-03-2009, 05:04 AM
  4. Generics Question
    By jdgallag in forum New To Java
    Replies: 8
    Last Post: 10-28-2008, 06:15 PM
  5. Question about java generics
    By Arrowx7 in forum New To Java
    Replies: 1
    Last Post: 08-14-2008, 02:37 AM

Posting Permissions

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