Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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 ++ ;
}
}
like image 951
Nour Avatar asked Mar 15 '13 21:03

Nour


3 Answers

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.

like image 113
rgettman Avatar answered Oct 11 '22 15:10

rgettman


I was having a similar problem, although I have a more complicated generic class hierarchy following the template pattern for OO programing. Where there is an interface then another interface extending that interface then an abstract class implementing that interface then classes extending the abstract class, but was getting the error "interface and a class. name clash: same erasure, yet neither overrides other" And found that only when I put or after every single class in the hierarchy and in every reference to that class would the error go away. For example:

public interface Set<U> {...}
public interface SetExtended<U> extends Set<U> {...}
public abstract class AbstractSetExtended<U> implements SetExtended<U>{...}
public class Set1<U> extends AbstractSetExtended<U> {...}
public class Set2<U> extends AbstractSetExtended<U> {...}

The template pattern is great for modular design, as well as factoring out common code and good for code reuse. To read a little more about the template pattern: https://en.wikipedia.org/wiki/Template_method_pattern

like image 43
Gabrielle Stewart Avatar answered Oct 11 '22 17:10

Gabrielle Stewart


Functional Interface -> First of all, Let's Understand the concept of @FuntionalInterface as we know that JDK-1.8
One New Concept Came known as Lambda Expersion :

Lambda Expersion

You need to understand first concept about LamdaExpersion then you can easily Get What is Funcation Interface role..

// Java program to demonstrate lambda expressions 
// to implement a user defined functional interface. 

// A sample functional interface (An interface with 
// single abstract method 
interface FuncInterface 
{ 
    // An abstract function 
    void abstractFun(int x); 

    // A non-abstract (or default) function 
    default void normalFun() 
    { 
    System.out.println("Hello"); 
    } 
} 

class Test 
{ 
    public static void main(String args[]) 
    { 
        // lambda expression to implement above 
        // functional interface. This interface 
        // by default implements abstractFun() 
        FuncInterface fobj = (int x)->System.out.println(2*x); 

        // This calls above lambda expression and prints 10. 
        fobj.abstractFun(5); 
    } 
} 

Lambda Expersion work only if your Define Interface have only 1 Method define because java compiler understand only if there is only one method in interface , then you dont need to denfine that method in Your Class, therefore : Anotation comes in picture that, when you want implement Lambda Expersion then use have to use @FunctionInterface in your Interface. If you, by mistake, write one more method in Your Interface, then Compiler will tell you that this is Functional Interface. Only One method will have to define to use Lamda Experssion in your Application

    @FunctionalInterface  
interface sayable{  
    void say(String msg);  
}  
public class FunctionalInterfaceExample implements sayable{  
    public void say(String msg){  
        System.out.println(msg);  
    }  
    public static void main(String[] args) {  
        FunctionalInterfaceExample fie = new FunctionalInterfaceExample();  
        fie.say("Hello there");  
    }  
}  
like image 1
Zafar Hussain Avatar answered Oct 11 '22 17:10

Zafar Hussain