Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

java - interface and a class. name clash: same erasure, yet neither overrides other

I have an interface, and when I try to implement one of its methods, I get this error : "name clash: enqueue(T#1) in GenericQueue and enqueue(T#2) in IGenericQueue have the same erasure, yet neither overrides the other where T#1 ,T#2 are type variables: T#1 extends Comparable declared in class GenericQueue T#2 extends Comparable declared in interface IGenericQueue " here's the code :

public interface IGenericQueue <T extends Comparable> {
public void enqueue(T j);
..
}

public class GenericQueue<T extends Comparable> implements IGenericQueue {
....

public void enqueue(T j) // the error is in this line.
{
    if(rear == maxSize -1)
        rear = -1; // means you have reached the last element start again ?

    queArray[++rear] = j;
    nItems ++ ;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your GenericQueue is implementing the raw interface IGenericQueue, so its T is different than the T in IGenericQueue. Add the <T> in the implements clause:

public class GenericQueue<T extends Comparable> implements IGenericQueue<T> {
//                                                                      ^^^

so you are implementing the generic interface with the same T.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...