Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic problem

 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to genercise my Entity interface :

So the getVersion method returns a typed Comparable.

I'm trying this :



The idea being that the getVersion method returns a Comparable object that is comparable with itself.

with concrete implementation :



I get an error saying I'm not implementing the getVersion method. Ack!
Anyone know how to solve this?

thanks, D.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"I get an error saying I'm not implementing the getVersion method. Ack!"

Are you sure? I tried compiling your code and did not get an error, just a warning:

C:\Jesper\workspace\Sandbox\src>javac -Xlint:unchecked org\jesperdj\sandbox\*.java
org\jesperdj\sandbox\Employee.java:5: warning: getVersion() in org.jesperdj.sandbox.Employee implements <T>getVersion() in org.jesperdj.sandbox.Entity; return type requires unchecked conversion
found : java.lang.Long
required: java.lang.Comparable<T>
public Long getVersion() {
^
1 warning
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the return type if fixed for each implementation of the interface, you should add the type parameter to the interface:



Also note that the above type parameter is the correct declaration of a "type that is comparable to itself". Your declaration was just a "type that is comparable to a (potentially different) comparable type", as far as I can tell.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic