| Author |
Generic problem
|
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
"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
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
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.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: Generic problem
|
|
|