Results 1 to 4 of 4
Thread: Quick question about generics
- 03-28-2011, 01:40 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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.
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.PHP Code:class Node<E extends Comparable<E>>
Thanks
-shaboinkin
-
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>();
- 03-28-2011, 01:58 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,543
- Rep Power
- 11
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.
- 03-28-2011, 01:59 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Generics Question
By erversteeg in forum New To JavaReplies: 3Last Post: 02-11-2011, 05:13 AM -
Question regarding generics
By Leaflord in forum Advanced JavaReplies: 9Last Post: 08-29-2009, 02:24 PM -
Generics & Inheritance Question
By Lee Rhodes in forum Advanced JavaReplies: 3Last Post: 07-03-2009, 05:04 AM -
Generics Question
By jdgallag in forum New To JavaReplies: 8Last Post: 10-28-2008, 06:15 PM -
Question about java generics
By Arrowx7 in forum New To JavaReplies: 1Last Post: 08-14-2008, 02:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks