• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using comprable interface between two classes  RSS feed

 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code that will make linked list and order its elements using self referential objects. but i have the following error:
incompatible types
required: ListNode<T#2>
found: ListNode<T#1>
where T#1,T#2 are type-variables:
T#1 extends Comparable declared in method <T#1>insertInOrder(T#1)
T#2 extends Comparable declared in class OrderedList

 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared a separate type parameter for your insertInOrder method, i.e, that method is itself a generic method. So, the type parameter T in method is different from the one used inside the class, but outside that method. T#1 is the placeholder created by the compiler for a type parameter. Since the two type parameters are different, that is why there are two different placeholders, and hence they are not compatible. To make it work, just remove the type parameter declaration from your method. Change it to:

Also,the bounds for your type parameter in the class should better be given as:

you should use generic Comparable, and not raw type. You must be getting an Unchecked warning there.

In fact, it would be even better to define it like this:

This makes this class to work with a subclass, which implements a Comparable of some super class.
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your reply is awesome and i tried it and works and what i get from you that the two place holders now are different as the compiler generates one for the generic method and one for the generic class and it needs only one.
I also tried the following definintion for the mehtod and it works either:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:Also,the bounds for your type parameter in the class should better be given as:You should use generic Comparable, and not raw type.

<nitpick>
Actually, the generics tutorial recommends:
<T extends Comparable<? super T>>
whenever you want a type that means "any Comparable".
</nitpick>

This is because Comparables are allowed to compare with types other than their own - indeed, legacy classes that haven't been "genericized" (ugh) will actually implement the equivalent of Comparable<Object>.

HIH

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Actually, the generics tutorial recommends:
<T extends Comparable<? super T>>
whenever you want a type that means "any Comparable".

You're right. Actually, I've mentioned this as well towards the end of my answer
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!