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

前端 未结 3 1511
囚心锁ツ
囚心锁ツ 2020-12-01 09:16

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

相关标签:
3条回答
  • 2020-12-01 09:23

    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");  
        }  
    }  
    
    0 讨论(0)
  • 2020-12-01 09:28

    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.

    0 讨论(0)
  • 2020-12-01 09:33

    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

    0 讨论(0)
提交回复
热议问题