1

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
    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.

Andrew
  • 13,757
  • 13
  • 66
  • 84
  • What compiler are you using? I'm not able to reproduce. – polygenelubricants Aug 05 '10 at 20:18
  • Eclipse IDE for Java Developers 1.3.0.20100617-0520 jdk1.0.6_13 – Andrew Aug 05 '10 at 20:23
  • `apply(T t)` is identical to `apply(F f)` during runtime (generics are checked by the compiler and transformed into `Object`, with appropriate casts inserted). I'm not currently able to test, though, and other answers have claimed it works, so I can't give a definitive answer -- leaving this as a comment instead. – Brian S Aug 05 '10 at 20:29
  • Awesome, awesome question. I am totally confused, because I find contradictions everywhere (e.g. the code in http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6182950 compiles fine for me). – polygenelubricants Aug 05 '10 at 21:00

3 Answers3

2

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:

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • Why is eclipse giving me this error then? Is it just an issue with the IDE? – Andrew Aug 05 '10 at 20:20
  • @Andrew: you are saying that the code from ideone.com, taken as a whole and put into one `Main` class, doesn't compile in your Eclipse? Which version? – polygenelubricants Aug 05 '10 at 20:22
  • Also related http://stackoverflow.com/questions/1827890/compiler-difference-between-intellij-and-eclipse – polygenelubricants Aug 05 '10 at 20:24
  • Sorry for the confusion, I'm saying that eclipse is showing this as an error, It still compiles and runs, but I don't understand why it is showing up as an error. – Andrew Aug 05 '10 at 20:25
  • @Andrew: if it's an error then it wouldn't compile and run. I'm confused now. We are talking about my version of the code on ideone.com, with the `k/key/v/value` correction, and the `return` statement in the body of the `@Override`, right? – polygenelubricants Aug 05 '10 at 20:34
  • Also related: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6182950 : methods clash algorithm should not depend on return type ; according to this, it seems that the code should NOT have compiled, if I'm reading this correctly... – polygenelubricants Aug 05 '10 at 20:37
  • 1
    I would think it should compile, given that one of the `apply` methods takes a `Foo` parameter while the other takes a `Two`. – ColinD Aug 05 '10 at 20:58
  • @ColinD: yes, the `javap` confirms this too, but the supposedly faulty code in the bug report compiles for me too, and the `javap` is absolutely insane with both `int f(List)` and `double f(List)` methods in the same class. I am utterly baffled, but I'm trying to investigate this as much as I can. But I do think your argument is correct... I hope... – polygenelubricants Aug 05 '10 at 21:03
  • @polygenelubricants When I copy your code into my eclipse an run it I get an error, Exception in thread "main" java.lang.Error: Unresolved compilation problems: Name clash: The method apply(F) of type Test.Function has the same erasure as apply(T) of type Test.Predicate but does not override it Name clash: The method apply(T) of type Test.Predicate has the same erasure as apply(F) of type Test.Function but does not override it at ... and it does not run. So yeah it does not compile for me locally. – Andrew Aug 05 '10 at 21:05
  • Ok, so jdk1.6.0_13 shows the error, but jdk1.6.0_20 does not, so something changed to fix it I guess – Andrew Aug 05 '10 at 21:12
  • 1
    @polygenelubricants Maybe I'm not understanding you correctly, but the situation from that bug report and the situation here are completely different. In the bug report, the two methods both take an argument that has the same erasure (`List`) where in this case the two methods take completely different types of arguments (`Foo` and `Two`). – ColinD Aug 05 '10 at 21:22
  • That said, you can make a class that `implements Predicate, Function` and then create an instance of it with `` or any identical types as `T` and `F`, resulting in two methods with the exact same signature... hm. – ColinD Aug 05 '10 at 21:46
0

What's going on is type erasure.

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

Skip Head
  • 7,580
  • 1
  • 30
  • 34
0

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

Andrew
  • 13,757
  • 13
  • 66
  • 84