开发者

Java Generics Name Clash error when extending multiple interfaces

开发者 https://www.devze.com 2023-01-10 05:06 出处:网络
I\'ve looked through similar questions and have come up short, so here it goes; public interface Predicate<T>{

I've looked through similar questions and have come up short, so here it goes;

public interface Predicate<T>{
    boolean apply(T t);
}
public interface Function<F, T>{
    T apply(F f);
}

public class ConcretePredicate extends Predicate<Foo>, Function<Bar, Boolean>{
    @Override
    public boolean apply(Foo foo){/*stuff*/}
    @Override
 开发者_Python百科   public Boolean apply(Bar bar){/*stuff*/}
} 

ConcretePredicate shows the error,

"Name clash: The method apply(T) of type Predicate has the same erasure as apply(F) of type Function but does not override it"

It looks like it should be working though, Anyone have any ideas as to what is going on?


[Edit] So it looks like this is an issue with eclipse, Galileo does not show this error, while Helios does. I've submitted a bug report with eclipse and will update once I get a response.

[Edit] Changed to a simpler case that shows the same error but removes confusion about erasure.


The code DOES work. Here it is slightly rewritten to be in one class for ideone.com:

interface Predicate<T>{
    boolean apply(T t);
}
interface Function<F, T>{
    T apply(F f);
}
class Foo {}
class Bar {}
class Two<K, V>{
    private K k;
    private V v;
    public Two(K k, V v) {
        this.k = k;
        this.v = v;
    }
    public K getKey() {return k;}
    public V getValue() {return v;}
}

Then here comes the good stuff:

abstract class NewPredicate
implements Predicate<Foo>, Function<Two<Foo, Bar>, Boolean>{
}

class ConcretePredicate extends NewPredicate{
    @Override
    public boolean apply(Foo foo){ return false; }
    @Override
    public Boolean apply(Two<Foo, Bar> two){ return null; }
} 

public class Main {
public static void main(String[] args) {
    System.out.println(new ConcretePredicate());
}
}

The code compiles and runs fine (as seen on ideone.com, Eclipse and javac 1.6.0_17).


Related questions

These are related to actual "has the same erasure as ... but does not override it" compiler error message:

  • Type erasure, overriding and generics
  • Overriding a method with Generic Parameters in Java?
  • Eclipse complains about name clash if an interface with a method having a generic argument is implemented.
  • Java Generics name clash, method not correctly overriden


What's going on is type erasure.

To get this to work you need to change the name of one of your apply methods.


I have submitted a bug report with the eclipse team and they are still arguing about the semantics of the JLS and what it means, but it would seem my issue is indeed a bug in eclipse Helios. Looks like I'm going to have to downgrade to Galileo until this is resolved.

Bugzilla Entry

0

精彩评论

暂无评论...
验证码 换一张
取 消