This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Generic problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic problem" Watch "Generic problem" New topic

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14599

"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\ 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 8 API documentation
Ilja Preuss

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
rocket mass heater kickstarter
subject: Generic problem