• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using comprable interface between two classes

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic