Getting error for generic interface: The interface Observer cannot be implemented more than once with different arguments:

点点圈 提交于 2019-11-28 01:55:17
Salman Paracha

Because of type erasure you can't implement the same interface twice (with different type parameters). So, the eclipse error that you are receiving is correct.

You could add a base class for all possible "T", which may be limiting and not useful depending on the scope of these classes. And, you have requested a solution that prevents you from creating a multitude of Observer classes (i am assuming interfaces) for every possible event, well I can't see how else you would do that without compromising compile time safety.

I would suggest the following

interface Observer<T>{
    public void update (T o);
}

interface DialogBoxAuthenticateObserver extends Observer<DialogBoxAuthenticate>{
}

The code clutter isn't horrible and if you place them all in one file, they will be easy to reference and maintain. Hope I have helped

EDIT: After some digging around on google (which pointed me back to stackoverflow!, your question was asked in a different fashion and answered similarly here

Composite must already implement Observer. Is that what really intended? You want this CompositeWordLists class to observe two ways at once?

Not sure this can help, but I came across the same Java compile error today.

I partially solved my case by extracting all the shared implementation I could get to a parametrized abstract class:

public enum Example {
    ;
    static interface Observer<T> { public void update (T o); }
    static abstract AbstractObserver<T> implements Observer<T> {
        public void update (T o) { /* shared stuff I can put here */ }
    }
    static Composite extends AbstractObserver<Parameter1> {
        public void update (T o) {
            super(o);
            /* type-specific code here */
        }
    }
    static CompositeWordLists extends AbstractObserver<Parameter2> {
        public void update (T o) {
            super(o);
            /* type-specific code here */
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!